Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何从laravel中的两个多对多关系中获得通用模型?_Php_Laravel_Laravel 4_Many To Many_Eloquent - Fatal编程技术网

Php 如何从laravel中的两个多对多关系中获得通用模型?

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', '=

共有3种型号:

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()

的相关型号,就像一个符咒一样!已将其编辑为包含在()中。谢谢非常感谢您的建议:)