【问题标题】:Filter tree structure based on children length | 3 level基于children长度过滤树结构| 3级
【发布时间】:2018-03-25 04:58:41
【问题描述】:

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

    [{
    "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 函数和关系数组来获取树结构。

【问题讨论】:

标签: php laravel


【解决方案1】:

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

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

【讨论】:

  • 是的,你是对的。数据来自数据库。但我想知道如果我的代码是这样的,我该如何使用。我已经更新了我的问题,请看一下。谢谢
  • @SuhailPallimalil 你没有显示任何代码,所以我帮不上忙。
  • @SuhailPallimalil 无法帮助您处理原始查询,抱歉。但我真的建议你学习 Eloquent,因为你可以在这里只用几行可读的代码来过滤数据,而不是使用巨大的 QB/raw 查询然后尝试过滤空数组。
【解决方案2】:

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

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

【讨论】:

    【解决方案3】:
    /**
     * 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;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-06-29
      • 1970-01-01
      • 1970-01-01
      • 2012-09-01
      • 1970-01-01
      • 2015-04-08
      • 1970-01-01
      • 2013-12-22
      相关资源
      最近更新 更多