2017-12-29 48 views
2

商品のコメントを表示したい。しかし、私がそれをすると、それは私の間違いを私に与えます。どうすれば修正できますか?Laravel- 5.5 App Comment :: userはリレーションシップインスタンスを返す必要があります。

私はproduct-commentsuser-comments

製品モデルbeetwen一対多の関係を使用しています。

public function comments(){ 
     return $this->hasMany('App\Comment','product_id','id'); 
    } 

ユーザモデル

public function comments() { 

     return $this->hasMany('App\Comment','user_id','id'); 
    } 

コメントモデルあなたが関係を返す必要が

public function user(){ 

     $this->belongsTo('App\User'); 
    } 

    public function product(){ 
     $this->belongsTo('App\Product'); 
    } 

ブレイドファイル

<figcaption class="text-center">{{$comment->user->username}}</figcaption> 

答えて

4

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

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

同じことがproduct()関係である:だからuser()関係定義方法にreturnを追加

関連する問題