Laravel :使用 Eloquent 在查询中加入
Laravel : Join within query using Eloquent
我有五个 table 需要从中获取数据。
post,product,categories,attributes,post_attribute
我的模型中有一个查询,它收集一个产品的所有 post,它包含类别和属性详细信息。
查询:
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"
}
]
},
在这个 table 中还有一个叫 post_attributes
的 table 我存储 attribute_id
和 product_id
属于 attribute
和 product
table.
我想过滤 attributes
仅在 post_attributes
table 中可用且仅适用于 'product' 的数据,在 post_attributes
[=37] 中可用=] 与 product_id
和 attribute_id
。
我该怎么做?
要约束预加载关系,您可以将闭包传递给预加载查询,要查询关系,您可以使用 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();
希望对您有所帮助。
我有五个 table 需要从中获取数据。
post,product,categories,attributes,post_attribute
我的模型中有一个查询,它收集一个产品的所有 post,它包含类别和属性详细信息。 查询:
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"
}
]
},
在这个 table 中还有一个叫 post_attributes
的 table 我存储 attribute_id
和 product_id
属于 attribute
和 product
table.
我想过滤 attributes
仅在 post_attributes
table 中可用且仅适用于 'product' 的数据,在 post_attributes
[=37] 中可用=] 与 product_id
和 attribute_id
。
我该怎么做?
要约束预加载关系,您可以将闭包传递给预加载查询,要查询关系,您可以使用 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();
希望对您有所帮助。