【发布时间】:2021-08-31 04:07:32
【问题描述】:
我有这个关系表:
-categories (has many products)
-products (has many features)
-features
我试图使用它的 slug 获得一个特殊的类别,并获得类别中的产品,我还想使用(id)通过某些功能过滤产品
我的代码:
$category = Category::whereHas('products', function ($query) {
$query->whereHas('features', function ($query2) {
$query2->where('id', 21); // Example id (products where has features with '21' id)
});
})->where('slug', 'category-slug')
->with('products:id,title', 'products.features')->get();
但代码会返回包含所有产品的该类别(有或没有 id 为 21 的功能)
解决办法是什么?
【问题讨论】:
-
代码只是在使用变量时不执行
$query2条件,还是在硬编码时也不运行? -
@MuhamadRafiPamungkas 是的。在每种模式下它都不起作用,它将返回类别中的所有产品
标签: php mysql laravel eloquent