laravel 我如何让所有属于父母的孩子口才?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38195241/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 14:05:45  来源:igfitidea点击:

How do I get all children that fall under a parent in eloquent?

laraveleloquent

提问by botmin

In my database, I have a Categories table. Categories can have parent categories, making it a recursive relationship

在我的数据库中,我有一个 Categories 表。类别可以有父类别,使其成为递归关系

I also have a products table. Each product falls under a category.

我也有一个产品表。每个产品都属于一个类别。

Say, for example, I have a tree that looks like this:

比如说,我有一棵树,看起来像这样:

Category
    Sub-Category 1
        Sub-Sub-Category 1
            Product 1
            Product 2
            Product 3
            Product 4
        Sub-Sub-Category 2
            Product 5
            Product 6
            Product 7
            Product 8
    Sub-Category 2
        Sub-Sub-Category 3
            Product 9
            Product 10
            Product 11
            Product 12

If I do $SubCategory1->products, I want it to give me Products 1-8

如果我这样做$SubCategory1->products,我希望它给我产品 1-8

If I do $SubSubCategory3->products, I want it to give me products 9-12

如果我这样做$SubSubCategory3->products,我希望它给我产品 9-12

If I do $Category->products, I want it to give me all products

如果我这样做$Category->products,我希望它给我所有的产品

Basically, I want the category to give all products that fall under it

基本上,我希望该类别提供属于它的所有产品

采纳答案by botmin

After hoping to find an answer that uses Laravel nicely, I ended up giving up and just writing the code to do what I wanted myself, and it ended up smaller than I anticipated.

在希望找到一个很好地使用 Laravel 的答案后,我最终放弃了,只是编写代码来做我自己想做的事情,结果比我预期的要小。

public function all_products()
{
    $products = [];
    $categories = [$this];
    while(count($categories) > 0){
        $nextCategories = [];
        foreach ($categories as $category) {
            $products = array_merge($products, $category->products->all());
            $nextCategories = array_merge($nextCategories, $category->children->all());
        }
        $categories = $nextCategories;
    }
    return new Collection($products); //Illuminate\Database\Eloquent\Collection
}

回答by Ketan Akbari

Suppose your Model name is Category

假设您的模型名称是 Category

Create a function on Category model

在类别模型上创建函数

public function children() { return $this->hasMany('App\Category', 'parent_id', 'id'); }

Using above method on your controller

在您的控制器上使用上述方法

$categories = Category::with('children')->where('parent_id',0)->get();

回答by Nixus

This way works very good:

这种方式非常有效:

class One extends Model {
    public function children()
    {
        return $this->hasMany(self::class, 'parent_id');
    }

    public function grandchildren()
    {
        return $this->children()->with('grandchildren');
    }
}

回答by Cerlin

please try the below Has Many Through relation and post the result

请尝试下面的 Has Many Through 关系并发布结果

class Category extends Model
{
    public function products()
    {
        return $this->hasManyThrough(
            'App\Product', 'App\Category',
            'parent_id', 'catergory_id', 'id'
        );
    }
}

Then you can use $category->products;to find your products

然后你可以用它$category->products;来查找你的产品

回答by Hossein Karami

you better use nested sets models. you should use this data structure due to get fastest way to query in mysql with a simple way. with just a parent_id attribute,you don't know how deep your tree is and this make problem in knowing how many join you need. I offer you this excellent article. managing-hierarchical-data-in-mysql

你最好使用嵌套集模型。您应该使用这种数据结构,因为它可以以一种简单的方式在 mysql 中获得最快的查询方式。只有一个 parent_id 属性,你不知道你的树有多深,这在知道你需要多少连接时会产生问题。我为您提供这篇出色的文章。管理分层数据在 mysql

for those who use laravel framework, I prefer to suggest this amazing package:Baum

对于那些使用 Laravel 框架的人,我更喜欢推荐这个神奇的包:Baum

回答by user3251285

Say $category_id = Category->id and you want a collection of products as children of that category, I would try:

说 $category_id = Category->id 并且您想要一系列产品作为该类别的子项,我会尝试:

$products = App\Product::with(['SubSubCategory.SubCategory.Category'=>function($query) use ($category_id) {$query->where('id', $category_id);}])->get();

$products = App\Product::with(['SubSubCategory.SubCategory.Category'=>function($query) use ($category_id) {$query->where('id', $category_id);}])->得到();

To be able to do so. You will need your 'one to many' inverse relationships to be as such:

能够这样做。您将需要这样的“一对多”反向关系:

//App\SubCategory

//应用\子类别

public function Category(){return $this->belongsTo('App\Category');}

public function Category(){return $this->belongsTo('App\Category');}

//App\SubSubCategory

//App\SubSubCategory

public function Sub_Category(){return $this->belongsTo('App\SubCategory');}

public function Sub_Category(){return $this->belongsTo('App\SubCategory');}

//App\Product

//应用\产品

public function SubSubCategory(){return $this->belongsTo('App\SubSubCategory');}

public function SubSubCategory(){return $this->belongsTo('App\SubSubCategory');}

Good luck.

祝你好运。