【问题标题】:Count the number of all nth children of all the root parent in Laravel Eloquent计算 Laravel Eloquent 中所有根父节点的所有第 n 个子节点的数量
【发布时间】:2015-06-25 20:45:11
【问题描述】:

我有一个来自 Laravel Eloquent 查询的集合。这里祖父有很多孩子(父亲),父亲有很多孩子(孙子)。 返回的结果是这样的:

    [
  {
    "grand_father_id": "26",
    "fathers": [
      {
        "father_id": "101",
        "children": [
          {
            "child_id": "77",
            "children_weight": [
              {
                "weight": "30.00",
              }
            ]
          },
          {
            "child_id": "84",
            "children_weight": [
              {
                "weight": "20.00",
              }
            ]
          }
        ]
      },
      {
        "father_id": "102",
        "children": [
          {
            "child_id": "78",
            "children_weight": [
              {
                "weight": "50.00",
              }
            ]
          }
        ]
      }
    ]
  },
  {
    "grand_father_id": "27",
    "fathers": [
      {
        "father_id": "100",
        "children": [
          {
            "child_id": "83",
            "children_weight": [
              {
                "weight": "100.00",
              }
            ]
          }
        ]
      }
    ]
  },
  {
    "grand_father_id": "28",
    "fathers": [
      {
        "father_id": "105",
        "children": [
          {
            "child_id": "81",
            "children_weight": [
              {
                "weight": "80.00",
              }
            ]
          },
          {
            "child_id": "82",
            "children_weight": [
              {
                "weight": "0.00",
              }
            ]
          }
        ]
      }
    ]
  },
  {
    "grand_father_id": "29",
    "fathers": [
      {
        "father_id": "108",
        "children": [
          {
            "child_id": "79",
            "children_weight": [
              {
                "weight": "44.00",
              }
            ]
          },
          {
            "child_id": "80",
            "children_weight": [
              {
                "weight": "56.00",
              }
            ]
          }
        ]
      }
    ]
  },
  {
    "grand_father_id": "30",
    "fathers": [
      {
        "father_id": "107",
        "children": [

        ]
      }
    ]
  }
]

我如何计算所有祖父的所有孙子的总数。我当然可以使用嵌套循环来计算这个。但是还有其他 Laravel Collection 方法可以做到这一点吗?顺便说一句,我在这里举的祖父、父亲、孙子的例子只是一个假名的例子。真正的字段是objectivesactionssuccess_indicatorssuccess_indicator_weight。获取集合的查询是:

return Objective::with([
        'actions' => function($query) {
        $query->select(['action_id', 'action_description', 'objective_id_fk']);
    }, 'actions.successIndicators' => function($query) {
        $query->select('success_indicator_id', 'success_indicator_description', 'action_id_fk');
    }, 'actions.successIndicators.SuccessIndicatorYearWeight' => function($query) {
        $query->select('success_indicator_weight', 'success_indicator_unit', 'success_indicator_id_fk');
    },
])->get()

【问题讨论】:

    标签: php laravel collections eloquent laravel-5


    【解决方案1】:

    有很多 array_* 辅助方法可用于对嵌套数组执行各种操作,但我认为没有任何东西可以满足您的需求。你可以自己检查 - http://laravel.com/docs/5.0/helpers#arrays

    我不知道 Collection 是否内置了任何东西,但您可以考虑每个 Grandfather 类的访问器方法:

    class Grandfather extends Eloquent
    {
        ...
        public function getTotalGrandchildrenAttribute()
        {
            $total_grandchildren = 0;
            // loop through each father
            foreach ($this->fathers as $father) {
                $total_grandchildren += count($father->children);
            }
            return $total_grandchildren;
        }
    }
    

    然后在你的脚本中:

    $grandfathers = Grandfathers::all();
    
    $total_grandchildren = 0;
    foreach ($grandfathers as $grandfather) {
        $total_grandchildren += $grandfather->total_grandchildren;
    }
    
    echo "Total grandchildren: $total_grandchildren";
    

    http://laravel.com/docs/5.0/eloquent#accessors-and-mutators

    此外,您可能希望使用with('fathers')with('children') 而不是使用上面显示的 all(),这样您就不会在每次想要获取父亲/孩子时都访问数据库:

    $grandfathers = Grandfathers::with(array('fathers' => function($query) {
        $query->with('children');
    });
    

    http://laravel.com/docs/5.0/eloquent#eager-loading

    【讨论】:

      最近更新 更多