简体   繁体   English

按供应商分类的产品 - laravel

[英]Category with product by vendor - laravel

I am working on a Laravel with MySQL e-commerce like website, and I can show all the products under its categories by vendor/store.我正在使用 MySQL 电子商务类网站开发 Laravel,我可以按供应商/商店显示其类别下的所有产品。

But the problem is, if I added a product under the same category, it also shows to other vendor which also have that category, that shouldn't have happen.但问题是,如果我在同一类别下添加了一个产品,它也会向也有该类别的其他供应商显示,这不应该发生。

My Query:我的查询:

$categories = Categories::with('products')
    ->whereHas('products', function ($query) use ($id) {
    $query->where('store_id' ,$id);
})
->get();

I think the problem is in the with part?我认为问题出在with部分? but I don't know exactly how to do it.但我不知道该怎么做。

DB Structure:数据库结构:

store

id  |  name


products

id | store_id  | category_id | name


categories
id | name

WhereHas() check if it has it, but does not filter the result. WhereHas()检查它是否有它,但不过滤结果。 Does it have a product with this store, if yes return all categories where you now includes products that does not filter.这家商店是否有产品,如果是,则返回您现在包含未过滤产品的所有类别。

There is an array syntax, for filtering with() call.有一个数组语法,用于过滤with()调用。 This will show the products correctly.这将正确显示产品。

Categories::with(['products' => function ($query) use ($id) {
    $query->where('store_id' ,$id);
}])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM