1
如何查询关系并仍然包含没有关系的模型?有两种型号laravel where has include no relation
商店
产品
代码
// Store
public function products() {
$this->belongsToMany('App\Store');
}
// Product
public function stores() {
$this->belongsToMany('App\Product');
}
和数据透视表连接它们称为product_store
。有些商店没有任何产品。如何查询所有的产品,即使是那些谁不属于任何商店,如:
Product::where('store.id', '=', 1)->get()
这是我目前如何做到这一点。
Product::whereHas('stores', function($query) {
$query->where('id', '=', $store_id);
});
检索所有产品与至少一个商店
如何过滤'$产品 - > stores'到只有一个。即$ store_id – bazi
此查询为您提供的商店商品是$ store_id –
是的。它也给了我没有任何商店的产品。但我也希望产品不在$ store_id和其他商店 – bazi