计算Laravel Eloquent中所有根父母的所有第n个孩子的数量

时间:2015-04-19 04:00:28

标签: php laravel collections eloquent laravel-5

我有一个来自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()

1 个答案:

答案 0 :(得分:1)

有大量的array_ *辅助方法可以对嵌套数组进行各种操作,但我不认为有任何东西可以为你的事业服务。您可以自己检查 - http://laravel.com/docs/5.0/helpers#arrays

我不知道自己是否内置了Collection,但你可以考虑每个祖父类的访问方法:

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