2016-11-08 20 views
1

で非オブジェクトのプロパティを取得しようとすると、これは、この私のPostモデルの私の記事のテーブルlaravel 5.2

<?php 
    use Illuminate\Database\Schema\Blueprint; 
    use Illuminate\Database\Migrations\Migration; 
    class CreatePostsTable extends Migration 
    { 

    public function up() 
    { 
     Schema::create('posts', function (Blueprint $table) { 
      $table->increments('id'); 

      $table->integer('user_id')->unsigned(); 
      $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade'); 

      $table->integer('prf_id')->unsigned(); 
      $table->foreign('prf_id')->references('id')->on('profiles')->onDelete('cascade'); 

      $table->longText('status'); 
      $table->timestamps(); 
     }); 
    } 


    public function down() 
    { 
     Schema::drop('posts'); 
    } 
} 

です:

<?php 

namespace App; 

use Illuminate\Database\Eloquent\Model; 

class Post extends Model 
{ 
    protected $table='posts'; 
    protected $fillable = ['status']; 


    protected $hidden = []; 

    public function profile(){ 
     return $this->belongsTo('App\Profile'); 
    } 

    public function user(){ 
     return $this->belongsTo('App\User'); 
    } 


} 

が、これは

プロファイルモデルです
<?php 

namespace App; 

use Illuminate\Database\Eloquent\Model; 

class Profile extends Model 
{ 
    protected $table='profiles'; 

    protected $fillable = ['user_id','name','position','roles','username','college','phone','location','graduation','skill']; 


    protected $hidden = []; 

    public function posts(){ 
     return $this->hasMany('App\Post'); 
    } 

} 

私は{{$ステータス - >をしようとすると、 this.itsが正しい値を示すが、{{$ステータス - > USER-> fnameは}} をしようとすると、これはUserモデル

<?php 

namespace App; 

use Illuminate\Foundation\Auth\User as Authenticatable; 

class User extends Authenticatable 
{ 

    protected $fillable = [ 
     'fname','lname', 'email','sex', 'password','user_id','roles' 
    ]; 

    /** 
    * The attributes that should be hidden for arrays. 
    * 
    * @var array 
    */ 
    protected $hidden = [ 
     'password', 'remember_token', 
    ]; 


    public function posts(){ 
     return $this->hasMany('App\Post'); 
    } 

} 

ですプロファイル:>ユーザー名}}これは、私にそれが表示されるたびに非オブジェクト(表示:C:\ xampp \ htdocs \ abc \ resources \ views \ pages \ profile.blade.php)のプロパティを取得しようとしています 実際にはわからない理由:(

+1

'$ status'をフェッチするコードを表示しますか? –

+0

'公開関数myprofile(Request $ request){ $ id = $ request-> prf; $ user = User :: where( 'id'、 '='、$ id) - > first(); $ u_id = $ user-> user_id; $ all = Profile :: where( 'id'、 '='、$ id) - > first(); $ tasks = Task :: where( 'user_id'、 '='、$ u_id) - > get(); $ user = User :: where( 'id'、 '='、$ id) - > first(); $ statuses = Post :: all(); ビューを戻します( 'pages.profile'、compact( 'all'、 'tasks'、 'user'、 'statuses')); } ' – leo

+0

@leoの編集で質問してください。 – apokryfos

答えて

1

Postクラスでは、次のコードを試してください:

public function profile(){ 
    return $this->belongsTo('App\Profile', 'prf_id'); 
} 
+0

sooo much.its working – leo

+0

ありがとうございます。この回答があなたの問題を解決した場合は、それを合格とマークしてください。 –