Product
и WishList
создают отношение Многие ко многим (по крайней мере для меня).В отношениях m---m
метод belongsToMany()
должен быть определен на обоих концах.
Если это предположение верно (схема отношений), определите ваши отношения следующим образом:
Product.php
public function wishLists()
{
return $this->belongsToMany('App\Models\WishList');
/**
* if you have a pivot table with a diferent name than: 'product_wish_list'
* use the second argument to indicate it:
* return $this->belongsToMany('App\Models\WishList', 'custom_table_name');
*/
}
WishList.php
public function products()
{
return $this->belongsToMany('App\Models\Product');
/**
* if you have a pivot table with a diferent name than: 'product_wish_list'
* use the second argument to indicate it:
* return $this->belongsToMany('App\Models\Product', 'custom_table_name');
*/
}
Итак, теперь в вашем контроллере:
WishListController.php
public function index()
{
$products = WishList::find(1)->products()->get();
return view('WishLists.index', compact('products'));
}