【问题标题】:Laravel- 5.5 App\Comment::user must return a relationship instance.Laravel- 5.5 App\Comment::user 必须返回一个关系实例。
【发布时间】:2017-12-29 21:29:49
【问题描述】:

我想展示产品 cmets。但是当我这样做时,它给了我以上错误。我该如何解决?

我正在使用一对多关系 beetwen product-commentsuser-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');
    }

刀片文件

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

【问题讨论】:

    标签: php laravel one-to-many laravel-5.5


    【解决方案1】:

    您需要返回关系。所以在user()关系定义方法中加入return

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

    product() 关系也是如此:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-11-27
      • 1970-01-01
      • 2018-05-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-17
      • 1970-01-01
      相关资源
      最近更新 更多