Laravel查询多个表格
使用Laravel 5.3,我有一个由用户,产品和愿望清单连接的数据库,由一个数据透视表product_wishlist加入。该模式如下:Laravel查询多个表格
USERS WISHLISTS PRODUCTS PRODUCT_WISHLIST
------------- ------------- -------------- ---------------
id id id product_id
name name name wishlist_id
user_id
在app/Wishlist.php
public function products()
{
return $this->belongsToMany(Product::class, 'product_wishlist');
}
public function owner()
{
return $this->belongsTo(User::class);
}
在app/product.php
public function wishlists()
{
return $this->belongsToMany(Whishlist::class, 'product_wishlist');
}
用户可以创建心愿,并增加产品心愿。
在产品页面中,我试图显示登录用户的所有愿望清单。
$wishlists = Auth::user()->wishlists;
但我也想retrive如果每个心愿包含产品(我将添加的每个心愿单附近的“加入收藏”按钮仅当此不包含产品还)。在Kabelo答案后
我编辑:
我要的是展示一个单一的产品页面,并在侧边栏显示已登录用户的所有心愿(每个用户都有自己的心愿):wishlist1(添加到列表),wishlist2(添加到列表),...但是,如果例如该页面的产品已经在wishlist2中,则不显示“(添加到列表)”。在这种情况下,我能证明“(从列表中删除)”
用户可以创建心愿和产品添加到心愿。
从你的问题,我看到一个用户可以创建多个愿望清单,这里有一个方法来实现这一点。
在应用/ Wishlist.php
class Wishlist extends Model
{
/**
* Get all of the products for the wishlist.
*/
public function products()
{
return $this->belongsToMany('App\Product', 'product_wishlist');
}
/**
* Get owner/creator of wishlist.
*/
public function owner()
{
return $this->belongsTo('App\User');
}
}
在应用/产品
class Product extends Model
{
/**
* Get the wishlists for the product.
*/
public function wishlists()
{
return $this->belongsToMany('App\Wishlist', 'product_wishlist');
}
}
在控制器功能
$products = App\Products::with(['wishlist'=> function($query){
$query->where('user_id', Auth::user()->id);
}])->get();
return view('product.index', compact('products'))
在视图/产品/ index.blade.php
@foreach($products as $product)
<div class="product">
<h3>{{ $product->name }}</h3>
@if(! count($product->wishlists))
<button>Add to wishlist</button>
@endif
</div>
@endforeach
要在单一产品页面
显示在控制器
public function show($id){
$_product = App\Products::where('id', $id)->first();
$wishlists = null;
$product_is_in_wishlist = false;
if(Auth::check()){
$wishlists = App\Wishlists::where('user_id', Auth::user()->id)
->with('products')->get();
foreach($wishlists as $wishlist){
if($wishlist->products->where('id', $_product->id)->count()){
$product_is_in_wishlist= true;
}
}
}
return view('product.show', compact('_product', 'wishlists', 'product_is_in_wishlist'));
}
鉴于/产品/ show.blade.php
<div id='product_container'>
<h1>{{ $_product->name }}</h1>
@if($product_is_in_wishlist)
<button>Remove from wishlist</button>
@else
<button>Add to wishlist</button>
@endif
</div>
<div id='wishlists_sidebar'>
<ul>
@foreach($wishlists as $wishlist)
<li>{{ $wishlist->name }}</li>
@endforeach
</ul>
</div>
谢谢Kabelo。我想要的是显示一个产品页面,并在侧边栏中显示登录用户的所有愿望清单(每个用户都有自己的愿望清单):wishlist1(添加到列表),wishlist2(添加到列表),...但如果例如该页面的产品已经在wishlist2中,然后不显示“(添加到列表)”。在这些情况下,我可以显示“(从列表中删除)” – user3670128
我刚刚添加了“添加到愿望清单”按钮逻辑。让我知道它走了。 –
很好,谢谢!我用'if($ wishlist-> products-> where('id',$ _product-> id)替换了'if($ wishlist-> products-> where('id',$ _product-> id))' > count())' – user3670128
您错过了白名单和产品之间的关系。有了这个,你只需要做一些类似'$ user-> wishlist->产品......'当然你可以数他们,并进行检查 – Mruf