samedi 21 juillet 2018

Laravel 5, Eloquent relationship/ Method does not exist

I have 3 tables in laravel 5.6

First Table

//Table1> users: id | name
//and in Model: User
public function bookmarks()
{
   return $this->hasMany(Bookmark::class);
}


Second Table

//Table 3> bookmarks: user_id  | bookmarkable_id  | bookmarkable_type     
// and in Model: Bookmark
public function user()
{
    return $this->belongsTo(User::class);
}

public function products()
{
    return $this->morphedByMany('App\Product', 'bookmarkable');
}


Third Table

//Table3> products: id | title | user_id
//and in Model: Product
public function user()
{
    return $this->belongsTo(User::class);
}

public function bookmarks()
{
    return $this->morphToMany('App\Bookmark', 'bookmarkable');
}

Now, I want return all bookmarked products for current user:

$products = auth()->user()->bookmarks()->product()->latest()->paginate(18);

But I get this error:

"Method Illuminate\Database\Query\Builder::products does not exist."

What is my wrong?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire