我对Eloquent和Laravel 5相对较新,并且正在使用查询构建器根据已提供给函数的变量为类别创建动态查询。
在我的类别表中,有一个名为“parent_id”的列,用于指示该类别是否为子类别。到目前为止,这是我的功能:
public static function filterCategory($vars) {
$query = Category::query();
if((array_key_exists('order_by', $vars)) && (array_key_exists('order', $vars))) {
$query = $query->orderBy($vars['order_by'], $vars['order']);
}
if(array_key_exists('product', $vars)) {
$query = $query->whereHas('products', function($q) use ($vars){
return $q->where('id', $vars['product']);
});
}
if(array_key_exists('sub_cats', $vars)) {
$query = $query->with('subCategories');
}
return $query->get();
}
我在Category模型上创建了一个名为getChildren的函数,如下所示:
public function subCategories() {
return $this->hasMany('App\Category', 'parent_id', 'id');
}
我想要做的是当我的类别函数被调用以返回类别时,我需要为每一行执行子查询,这将在结果对象上创建包含所有类别的子数组当前类别的id,因为它是'parent_id'。 我想要这个回复:
[
{
"id": "1",
"name": "Dr. Ben Becker DDS",
"description": "Et atque illo ut architecto. Blanditiis laboriosam hic sed quia. Aperiam quis totam distinctio.",
"created_at": "1981-04-07 22:04:40",
"updated_at": "2009-02-26 19:53:09",
"parent_id": null,
"subcategories" : {
//results in here
}
},
{
"id": "2",
"name": "Ms. Winona Lehner",
"description": "Repudiandae nostrum repellendus nulla delectus saepe sapiente ipsam. Delectus voluptatem quis excepturi. Tenetur nostrum et cum quia. Fugiat totam sed dicta totam illo ut dolores.",
"created_at": "2002-01-13 13:42:18",
"updated_at": "1981-10-25 11:39:14",
"parent_id": null,
"subcategories" : {
//results in here
}
},
现在,这对于我在vanilla PHP和MYSQL中做的很简单,但我不知道如何在Laravel中使用Eloquent查询构建器实现这一点。
**更新 所以我有子类别返回,但遗憾的是不正确。这是我的输出:
[
{
"id": "1",
"name": "Dr. Ben Becker DDS",
"description": "Et atque illo ut architecto. Blanditiis laboriosam hic sed quia. Aperiam quis totam distinctio.",
"created_at": "1981-04-07 22:04:40",
"updated_at": "2009-02-26 19:53:09",
"parent_id": null,
"sub_categories": [
{
"id": "3",
"name": "Dr. Manuel Medhurst",
"description": "Pariatur ut corporis quas sequi dolor totam. Nisi ad amet velit reiciendis voluptates dolore aperiam. Impedit perferendis et a.",
"created_at": "1998-09-20 07:40:23",
"updated_at": "2002-04-01 18:41:24",
"parent_id": "1"
},
{
"id": "4",
"name": "Lee Monahan",
"description": "Nihil qui quod voluptatem non accusamus voluptas. Non et quae velit delectus consequatur vel. Qui totam non rerum sunt nisi soluta sequi. Consequatur eaque quia delectus qui assumenda.",
"created_at": "2014-11-24 06:31:10",
"updated_at": "1996-03-28 18:58:55",
"parent_id": "1"
},
{
"id": "5",
"name": "Miller Osinski",
"description": "Itaque et sed voluptatem earum nulla eligendi tenetur. Et velit ut voluptas similique. Autem rerum sapiente voluptatibus optio esse qui sit et. Alias eligendi aut quibusdam inventore.",
"created_at": "2011-10-08 18:16:56",
"updated_at": "1996-09-29 16:29:37",
"parent_id": "1"
}
]
},
{
"id": "3",
"name": "Dr. Manuel Medhurst",
"description": "Pariatur ut corporis quas sequi dolor totam. Nisi ad amet velit reiciendis voluptates dolore aperiam. Impedit perferendis et a.",
"created_at": "1998-09-20 07:40:23",
"updated_at": "2002-04-01 18:41:24",
"parent_id": "1",
"sub_categories": []
},
{
"id": "4",
"name": "Lee Monahan",
"description": "Nihil qui quod voluptatem non accusamus voluptas. Non et quae velit delectus consequatur vel. Qui totam non rerum sunt nisi soluta sequi. Consequatur eaque quia delectus qui assumenda.",
"created_at": "2014-11-24 06:31:10",
"updated_at": "1996-03-28 18:58:55",
"parent_id": "1",
"sub_categories": []
},
{
"id": "5",
"name": "Miller Osinski",
"description": "Itaque et sed voluptatem earum nulla eligendi tenetur. Et velit ut voluptas similique. Autem rerum sapiente voluptatibus optio esse qui sit et. Alias eligendi aut quibusdam inventore.",
"created_at": "2011-10-08 18:16:56",
"updated_at": "1996-09-29 16:29:37",
"parent_id": "1",
"sub_categories": []
},
{
"id": "2",
"name": "Ms. Winona Lehner",
"description": "Repudiandae nostrum repellendus nulla delectus saepe sapiente ipsam. Delectus voluptatem quis excepturi. Tenetur nostrum et cum quia. Fugiat totam sed dicta totam illo ut dolores.",
"created_at": "2002-01-13 13:42:18",
"updated_at": "1981-10-25 11:39:14",
"parent_id": null,
"sub_categories": []
}
]
首先,这看起来是正确的,但在我的表中,类别1只有1个孩子(类别4),类别2应该只有2个孩子(第3行和第5行)。似乎所有这些子类别都被错误地放入第1行的子类别数组中?
由于
答案 0 :(得分:3)
将类别模型中的关系添加到自身,如下所示:
public function subcategories()
{
return $this->hasMany('App\Category', 'parent_id', 'id');
}
更改类别模型名称空间(如果它不是应用程序。
)然后你就可以像这样查询它们:
$categories = Category::with('subcategories')->get();
如果您需要,您也可以按parent_id分组以安排它们:$categories->groupBy( 'parent_id' )->toJson();