如何删除 Laravel 中 parent-child 关系中的空 children 元素?

How to remove empty children element in parent-child relation in Laravel?

我正在研究 laravel 中的递归 parent-child 关系,并且我已经成功地从 eloquent.

中递归地获得了 parent-child

我遇到一个问题,当 parent 或 child 没有 child 时,它会在我的 json 中显示 children: []。我想删除空 children 元素 children: []。因此,如果 parent 或 child 没有 child,则不应显示 children: []。我会附上它的照片。

我的eloquent模特:

 public function allChild () {
        return $this->hasMany(self::class, 'parent_id', 'id')->select('id', 'parent_id', 'category_name as label');
    }
public function children () {
        return $this->allChild()->with('children');
    }

我的控制器

 $categories = CatalogCategories::select('id', 'category_name as label')->where('parent_id', 0)
                ->with('children')->get();

现在的结果

[
    {
        "id":1,
        "label":"Mainan",
        "children":[{
            "id":4,
            "parent_id":1,
            "label":"Category shoes",
            "children":[{
                "id":18,
                "parent_id":4,
                "label":"test",
                "children":[
                    {
                        "id":25,
                        "parent_id":18,
                        "label":"sub cat tes",
                        "children":[
                            {
                                "id":25,
                                "parent_id":18,
                                "label":"sub cat tes",
                                "children":[]
                            }
                        ]
                    },
                    {
                        "id":27,
                        "parent_id":18,
                        "label":"testtttt 123",
                        "children":[]
                    }
                ]
            }]
        }]
    }
]

我想要的结果

[
    {
        "id":1,
        "label":"Mainan",
        "children":[{
            "id":4,
            "parent_id":1,
            "label":"Category shoes",
            "children":[{
                "id":18,
                "parent_id":4,
                "label":"test",
                "children":[
                    {
                        "id":25,
                        "parent_id":18,
                        "label":"sub cat tes",
                        "children":[
                            {
                                "id":25,
                                "parent_id":18,
                                "label":"sub cat tes"
                            }
                        ]
                    },
                    {
                        "id":27,
                        "parent_id":18,
                        "label":"testtttt 123"
                    }
                ]
            }]
        }]
    }
]

我找到了解决办法,我把Laravel的JSON输出保持这样,用JS去掉空的children:[]

我用javascript消除空children

deleteEmpty(data){               
     for (let index = 0; index <data.length; index++) {                    
       if (data[index].children.length != 0){
          this.deleteEmpty(data[index].children);
       } else {                        
          delete data[index]['children']
       }
     }                
   return data;
}

使用has()

检索至少有一条评论的所有帖子:

$posts = Post::has('comments')->get();

https://laravel.com/docs/8.x/eloquent-relationships#querying-relationship-existence