[英]How can I do this with Eloquent Relationship
I'm working on an Online Store project and I wanted to create Add To Favourite system for users to add products that they like to their favourite list.我正在处理一个在线商店项目,我想创建添加到收藏夹系统,以便用户将他们喜欢的产品添加到他们最喜欢的列表中。
So I have created a table named favourite_products
which goes like this:所以我创建了一个名为
favourite_products
的表,如下所示:
So the usr_id
stands for user id and prd_id
stands for product id.所以
usr_id
代表用户 id, prd_id
代表产品 id。
Here is the User.php
Model:这是
User.php
Model:
public function favourites()
{
return $this->belongsToMany(Product::class, 'favourite_products', 'usr_id', 'prd_id')->withTimestamps();
}
And this the Product.php
Model:这是
Product.php
Model:
public function favouritees()
{
return $this->belongsToMany(User::class, 'favourite_products', 'prd_id', 'usr_id');
}
Now I wanted to get all the data from this table.现在我想从这个表中获取所有数据。
So I tried adding this to the Controller:所以我尝试将它添加到 Controller 中:
public function index()
{
$favouriteProducts = FavouriteProduct::all();
return view('admin.shop.favourites.all', compact('favouriteProducts'));
}
Then in the Blade:然后在刀片中:
@foreach($favouriteProducts as $fav_pro)
@php
$member_info = \App\User::where($fav_pro->usr_id)->first();
$product_info = \App\Shop\Product::find($fav_pro->prd_id);
@endphp
<tr class="closest-tr">
<td>{{ ++$menuCounter }}</td>
<td>{{ $member_info->usr_name }}</td>
<td>{{ $product_info->prd_name }}</td>
</tr>
@endforeach
But I wanted to know that, how can I retrieve usr_name
& prd_name
by User and Product Models relationships?但我想知道,如何通过用户和产品模型关系检索
usr_name
和prd_name
? Therefore, I won't need to add the Model names in the view and get data there...因此,我不需要在视图中添加 Model 名称并在那里获取数据......
I tried these two at the Controller, but these are both wrong:我在 Controller 上试过这两个,但这些都是错误的:
Product::whereHas('favouritees')->get(); // if two different users add same product, it returns only one of them
User::whereHas('favourites')->get(); // if a user add different products, it returns only one of them for the user
So the question is, how can I show all the results from favourite_products
at a table with User or Product Many To Many relationship?所以问题是,如何在具有用户或产品多对多关系的表中显示
favourite_products
的所有结果?
As you are using the pivot model, you can add these relations in your FavouriteProduct
model:当您使用 pivot model 时,您可以在您的
FavouriteProduct
产品 model 中添加这些关系:
public function user()
{
return $this->belongsTo(User::class, 'usr_id');
}
public function product()
{
return $this->belongsTo(Product::class, 'prd_id');
}
now in your blade file:现在在你的刀片文件中:
@foreach($favouriteProducts as $fav_pro)
<tr class="closest-tr">
<td>{{ ++$menuCounter }}</td>
<td>{{ $fav_pro->user->usr_name }}</td>
<td>{{ $fav_pro->product->prd_name }}</td>
</tr>
@endforeach
also don't forget to add with
to your query in your controller也不要忘记在您的 controller 中
with
您的查询
public function index()
{
$favouriteProducts = FavouriteProduct::with('product', 'user')->all();
return view('admin.shop.favourites.all', compact('favouriteProducts'));
}
To get all the data (product and users that favorite that product) get all the product with the users related to them获取所有数据(产品和最喜欢该产品的用户)获取所有产品以及与其相关的用户
$products = Product::whereHas('favouritees')->with('favouritees')->get();
// if two different users add same product, it returns only one of them but it will have two users in the relation favouritees
Then in you blade you can loop them然后在你的刀片中你可以循环它们
@foreach($products as $product)
@foreach($product->favouritees as $user)
<tr class="closest-tr">
<td>{{ ++$menuCounter }}</td>
<td>{{ $user->usr_name }}</td>
<td>{{ $product->prd_name }}</td>
</tr>
@endforeach
@endforeach
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.