user9127206
user9127206

Reputation:

Laravel- 5.5 App\Comment::user must return a relationship instance.

I want to display products comments. But When I do that, it gives me above error. How can I fix that ?

I'm using One To Many relationship beetwen product-comments and user-comments

Product model;

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

User Model;

public function comments() {

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

Comment Model;

public function user(){

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

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

Blade file

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

Upvotes: 3

Views: 3556

Answers (1)

Alexey Mezenin
Alexey Mezenin

Reputation: 163798

You need to return relationship. So add return to the user() relationship definition method:

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

The same is with the product() relationship:

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

Upvotes: 6

Related Questions