Laravel- 5.5 App\Comment::用户必须 return 一个关系实例。
Laravel- 5.5 App\Comment::user must return a relationship instance.
我要显示商品评论。但是当我这样做时,它给了我上述错误。我该如何解决?
我在 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');
}
评论模型;
public function user(){
$this->belongsTo('App\User');
}
public function product(){
$this->belongsTo('App\Product');
}
Blade 文件
<figcaption class="text-center">{{$comment->user->username}}</figcaption>
你需要return关系。所以在user()
关系定义方法中加入return
:
public function user()
{
return $this->belongsTo('App\User');
}
与product()
关系相同:
public function product()
{
return $this->belongsTo('App\Product');
}
我要显示商品评论。但是当我这样做时,它给了我上述错误。我该如何解决?
我在 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');
}
评论模型;
public function user(){
$this->belongsTo('App\User');
}
public function product(){
$this->belongsTo('App\Product');
}
Blade 文件
<figcaption class="text-center">{{$comment->user->username}}</figcaption>
你需要return关系。所以在user()
关系定义方法中加入return
:
public function user()
{
return $this->belongsTo('App\User');
}
与product()
关系相同:
public function product()
{
return $this->belongsTo('App\Product');
}