基于子长度的过滤树结构| 3级

时间:2017-10-13 06:01:41

标签: php laravel

根据子项长度过滤树结构 在下面的树结构中我想删除数组,如果子项的子项长度为零。有没有办法没有使用多个循环和构建新数组?

    [{
    "id": 1,
    "name": "XYZ",
    "type": 1,
    "mapping_id": 1,
    "children": [
        {
            "id": 1,
            "name": "XYZ UAE",
            "brand_id": 1,
            "type": 2,
            "mapping_id": 2,
            "children": [
                {
                    "id": 1,
                    "name": "Dubai Airport Free Zone",
                    "country_id": 228,
                    "brand_region_id": 1,
                    "type": 3,
                    "mapping_id": 3
                }
            ]
        },
        {
            "id": 3,
            "name": "test",
            "brand_id": 1,
            "type": 2,
            "mapping_id": 0,
            "children": []
        }
    ]
},
{
    "id": 2,
    "name": "ABC",
    "type": 1,
    "mapping_id": 0,
    "children": [
        {
            "id": 2,
            "name": "ABC Restaurants UAE",
            "brand_id": 2,
            "type": 2,
            "mapping_id": 0,
            "children": []
        }
    ]}]

我的拉取数据的代码是

 $assets = $this->brand
        ->select('brands.id', 'brands.name', DB::raw('1 as type,IFNULL(supplier_asset_mappings.id,0) as mapping_id'))
        ->leftJoin('supplier_asset_mappings', function ($join) use ($supplierId) {
            $join->on('asset_id', '=', 'brands.id')
                ->where('supplier_asset_mappings.supplier_id', $supplierId)
                ->where('supplier_asset_mappings.asset_type', 1);
        })
        ->with(array('children' => function ($query) use ($supplierDeliveryCountries, $supplierId) {
            $query->select('brand_regions.id', 'brand_regions.name', 'brand_id', DB::raw('2 as type,IFNULL(supplier_asset_mappings.id,0) as mapping_id'))
                ->leftJoin('supplier_asset_mappings', function ($join) use ($supplierId) {
                    $join->on('asset_id', '=', 'brand_regions.id')
                        ->where('supplier_asset_mappings.supplier_id', $supplierId)
                        ->where('supplier_asset_mappings.asset_type', 2);
                })
                ->where('status', '=', BrandRegion::STATUS_ACTIVE);
        }, 'children.children' => function ($query) use ($supplierDeliveryCountries, $supplierId) {
            $query->select('branches.id', 'branches.name', 'country_id', 'brand_region_id', DB::raw('3 as type,IFNULL(supplier_asset_mappings.id,0) as mapping_id'))
                ->leftJoin('supplier_asset_mappings', function ($join) use ($supplierId) {
                    $join->on('asset_id', '=', 'branches.id')
                        ->where('supplier_asset_mappings.supplier_id', $supplierId)
                        ->where('supplier_asset_mappings.asset_type', 3);
                })
                ->where('branches.location_type', '=', 1)//location type is 1 for branch
                ->whereIn('country_id', $supplierDeliveryCountries)
                ->where('status', '=', Branch::STATUS_ACTIVE);
        }))
        ->where('brands.company_id', $companyId)
        ->where('brands.status', '=', Brand::STATUS_ACTIVE)
        ->get();

这里我使用with函数和关系数组来获取树结构。

3 个答案:

答案 0 :(得分:0)

它看起来像来自DB的数据,所以你应该在从DB获取数据时使用Eloquent has()方法。只有在指定了关系时才会加载模型。

或者,您可以在Laravel集合上使用filter()或在数组上使用array_filter()

答案 1 :(得分:0)

纯php替代方案:您可以利用函数array_walk_recursive和参考传递的参数来检查每个节点并过滤掉树的空叶。

http://php.net/manual/en/function.array-walk-recursive.php

答案 2 :(得分:0)

/**
 * filter categories
 *
 * @param Builder $query
 * @param int $counter
 * @return Builder
 */
public function scopeFilter(Builder $query, $counter = 3)
{
    $label = request('label');
    $title = request('title');

    if (isset($label)) {
        $query->where('label', 'like', '%' . $label . '%');
    }

    if (isset($title)) {
        $query->where('title', 'like', '%' . $title . '%');
    }

    if ((isset($label) || isset($title)) && $counter > 0) {
        $counter -= 1;
        $query->orWhereHas('children', function (Builder $query) use ($counter) {
            return $query->filter($counter);
        });
    }

    return $query;
}