我有五个表,我需要从中获取所有这些数据。
post,product,categories,attributes,post_attribute
我的模型中有一个查询,它收集一个产品的所有帖子,它包含类别和属性详细信息。 查询:
Post::with(['product.categories.attributes'])->whereStatus("Active")->get();
响应:
"PostDetails": [
{
"id": 153,
"user_id": 3,
"product_id": 1,
"demand_or_supply": "Demand",
"description": "This is a post description three",
"image_one": null,
"image_two": null,
"image_three": null,
"image_four": null,
"status": "Active",
"created_at": "2018-05-07 09:51:08",
"updated_at": "2018-05-07 09:51:08",
"product": {
"id": 1,
"title": "Diamond",
"icon": null,
"status": "Active",
"created_at": "2018-04-27 18:46:28",
"updated_at": "2018-04-27 18:46:28",
"categories": [
{
"id": 1,
"product_id": 1,
"title": "Shape",
"status": "Active",
"sort_order": 1,
"created_at": "2018-04-27 18:46:28",
"updated_at": "2018-04-27 18:46:28",
"attributes": [
{
"id": 1,
"product_id": 1,
"category_id": 1,
"parent_id": null,
"title": "Round",
"status": "Active",
"sort_order": 2,
"created_at": "2018-04-27 18:46:29",
"updated_at": "2018-04-27 18:46:29"
},
{
"id": 2,
"product_id": 1,
"category_id": 1,
"parent_id": null,
"title": "Princess",
"status": "Active",
"sort_order": 1,
"created_at": "2018-04-27 18:46:29",
"updated_at": "2018-04-27 18:46:29"
},
{
"id": 3,
"product_id": 1,
"category_id": 1,
"parent_id": null,
"title": "Oval",
"status": "Active",
"sort_order": 3,
"created_at": "2018-04-27 18:46:29",
"updated_at": "2018-04-27 18:46:29"
},
{
"id": 4,
"product_id": 1,
"category_id": 1,
"parent_id": null,
"title": "Kite",
"status": "Active",
"sort_order": 8,
"created_at": "2018-04-27 18:46:29",
"updated_at": "2018-04-27 18:46:29"
},
{
"id": 5,
"product_id": 1,
"category_id": 1,
"parent_id": null,
"title": "1-5",
"status": "Active",
"sort_order": 4,
"created_at": "2018-04-27 18:46:29",
"updated_at": "2018-04-27 18:46:29"
},
{
"id": 6,
"product_id": 1,
"category_id": 1,
"parent_id": null,
"title": "6-10",
"status": "Active",
"sort_order": 9,
"created_at": "2018-04-27 18:46:29",
"updated_at": "2018-04-27 18:46:29"
},
{
"id": 8,
"product_id": 1,
"category_id": 1,
"parent_id": null,
"title": "-2",
"status": "Active",
"sort_order": 10,
"created_at": "2018-04-27 18:46:29",
"updated_at": "2018-04-27 18:46:29"
},
{
"id": 9,
"product_id": 1,
"category_id": 1,
"parent_id": null,
"title": "+2 -4",
"status": "Active",
"sort_order": 7,
"created_at": "2018-04-27 18:46:29",
"updated_at": "2018-04-27 18:46:29"
},
{
"id": 10,
"product_id": 1,
"category_id": 1,
"parent_id": null,
"title": "+4 -6 1/2",
"status": "Active",
"sort_order": 6,
"created_at": "2018-04-27 18:46:29",
"updated_at": "2018-04-27 18:46:29"
}
]
},
此表中还有另一个名为post_attributes
的表,我存储了attribute_id
和product_id
,它属于attribute
和product
表。
我想过滤attributes
数据,这些数据仅在post_attributes
表格中提供,且仅适用于' product'可在post_attributes
表格中使用product_id
和attribute_id
获取。
我怎么能这样做?
答案 0 :(得分:3)
要约束急切的加载关系,您可以将闭包传递给预先加载的查询,并查询可以使用whereHas
函数的关系,所以它看起来像这样:
Post::with(['product.categories.attributes' => function($query) {
// Eager load constraint
$query->whereHas('post_attribute', function ($query) {
$query->where('product_id', 1); // Filter by the joined data
});
}])->whereStatus("Active")->get();
希望这会对你有所帮助。