Php 如何从laravel中的两个多对多关系中获得通用模型?
共有3种型号:Php 如何从laravel中的两个多对多关系中获得通用模型?,php,laravel,laravel-4,many-to-many,eloquent,Php,Laravel,Laravel 4,Many To Many,Eloquent,共有3种型号: Products belongstoMany Categories Products belongstoMany Stores Categories belongstoMany Products Stores belongstoMany Products 如何找到属于特定类别和特定商店的所有产品 这就是我尝试过的: Product::getAll()->join('categorie_product', 'categorie_product.product_id', '=
Products belongstoMany Categories
Products belongstoMany Stores
Categories belongstoMany Products
Stores belongstoMany Products
如何找到属于特定类别和特定商店的所有产品
这就是我尝试过的:
Product::getAll()->join('categorie_product', 'categorie_product.product_id', '=', 'product.id')
->join('categorie', 'categorie.id', '=', 'categorie_product.categorie_id')
->where('categorie.name', '=', $categorieName)
->paginate(10);
有什么建议吗?谢谢
附言:我想用雄辩来完成这项任务。DB::*可以,但我需要有说服力的实现,因为我正在使用Dingo API来完成一些REST API。使用
whereHas
按关系过滤:
Product::with('categorie','store')->whereHas('categorie', function($q) use ($categoryName){
$q->where('categories.name', $categoryName);
})->whereHas('store', function($q) use ($storeId){
$q->where('stores.id', $storeId);
})->paginate(10);
另外,不要忘了使用
with()
的相关型号,就像一个符咒一样!已将其编辑为包含在()中。谢谢非常感谢您的建议:)