简体   繁体   English

Laravel中多级查询

[英]Query of multi level in Laravel

I have a Category / Products / Users I need to get all Categories of All Products for a specific user. 我有一个类别/产品/用户,我需要为特定用户获取所有产品的所有类别。 I was able to make selecting a product for a specific User but I couldn't tell how to get all the categories of these products. 我能够为特定用户选择产品,但我不知道如何获取这些产品的所有类别。

Products and Users relationship is ManyToMany. 产品与用户的关系是ManyToMany。 The relationship between product belongsTo Category and Category hasMany Products 产品所属类别与类别之间的关系有很多产品

Here is what I made: 这是我做的:

$products = Product::byUser(auth()->id())->get();

public function scopeByUser(Builder $query)
{
    return $query->whereHas('users', function ($q) {
        $q->where('id', auth()->id());
    });
}

But I couldn't make it in a way to have: 但是我无法做到:

$products = Category::productsByUser(auth()->id());

So that I can get all the categories and all the products of the loggedIn user. 这样我就可以获得登录用户的所有类别和所有产品。

Thank you, 谢谢,

You can either load the categories with the products in a single query to get a list of the products associated to a user. 您可以在单个查询中加载产品类别,以获取与用户关联的产品列表。 Or you can create a scope filter on the category model to return a list of categories associated with products that are associated with the user: 或者,您可以在类别模型上创建范围过滤器,以返回与与用户相关联的产品相关联的类别列表:

Load categories with() products: 用()产品加载类别:

Eager Loading Relationships 渴望加载关系

$products = Product::with('categories')->byUser(auth()->user()->id)->get();

This will return a collection of products which already have the related categories loaded so you can call $products->first()->categories()->get() to return a list of the categories for that product. 这将返回已经加载了相关类别的产品集合,因此您可以调用$ products-> first()-> categories()-> get()返回该产品类别的列表。

Use a scope filter on the Category Model to list categories only: 在类别模型上使用范围过滤器仅列出类别:

Query Scopes 查询范围

public function scopeByUser(Builder $query, $user_id)
{
    return $query->whereHas('products', function ($q) use($user_id) {
        $q->whereHas('user', function ($q) use($user_id){
            $q->where('id', $user_id);
        }
    });
}

Then you can run Category::byUser(auth()->user()->id)->get(); 然后您可以运行Category :: byUser(auth()-> user()-> id)-> get(); to return a collection of categories which are associated to products which are associated to user id given. 返回与给定用户ID相关联的产品相关联的类别的集合。

UPDATE: Conditional Eager Loading with Scope filter 更新:有条件的急切加载与范围过滤器

You still want to user eager loading with a condition to load all products which are associated to the user and then apply the above scope filter to limit the categories returned to only be those associated to the user: 您仍然希望用户急切地加载一个条件,以加载与该用户相关联的所有产品,然后应用上述范围过滤器将返回的类别限制为仅与该用户相关联的类别:

$user_id = auth()->user()->id;
$categories = Category::with(['products'=> function($q) use($user_id){
    $q->whereHas('user',function($q) use($user_id){
        $q->where('id',$user_id);
    });
}])->byUser($user_id)->get()

This will return a collection of Categories which are associated to products which are associated to the provided user id and will eager load products for the category if those products are associated with the user id provided. 这将返回与产品相关联的类别的集合,这些产品与提供的用户ID相关联,并且如果这些产品与所提供的用户ID相关联,则会急于为该类别加载产品。

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

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