[英]Laravel : Join within query using Eloquent
我有五個表,我需要從這些表中獲取數據。 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
和product
表的attribute_id
和product_id
。
我想過濾僅在post_attributes
表中可用的attributes
數據,以及僅對在post_attributes
中具有product_id
和attribute_id
可用的'product'的attribute_id
。 我怎樣才能做到這一點 ?
要約束緊急加載關系,您可以將閉包傳遞給whereHas
加載查詢,並且可以使用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();
希望這對您有所幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.