Laravel- 5.5 App \ Comment :: user必须返回一个关系实例。

问题描述 投票:2回答:1

我想展示产品评论。但是,当我这样做时,它给了我上面的错误。我该如何解决这个问题?

我正在使用qazxsw poi和qazxsw poi之间的One To Many关系

产品型号;

product-comments

用户模型;

user-comments

评论模型;

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

刀片文件

public function comments() {

         return $this->hasMany('App\Comment','user_id','id');
     }
php laravel one-to-many laravel-5.5
1个回答
5
投票

你需要回归关系。所以将public function user(){ $this->belongsTo('App\User'); } public function product(){ $this->belongsTo('App\Product'); } 添加到<figcaption class="text-center">{{$comment->user->username}}</figcaption> 关系定义方法中:

return

user()关系相同:

public function user()
{
    return $this->belongsTo('App\User');
}
© www.soinside.com 2019 - 2024. All rights reserved.