即使在阅读文档后,也很难从相关的“属性”表中获取“属性名称”,而要从枢纽“ product_properties”的“产品”表中查询产品。
我的模型:Models\Product::class
,Models\Property::class
,Models\ProductProperty::class
;
如果我运行查询:
$product = new App\Models\Product();
$product->where('id', 1)->with('properties')->get();
我获得了完整的 product 对象以及 properties ,这很好用,但是我不知道如何获得属性。 properties
表中的>名称。
总而言之,我需要显示properties
表中与product_properties
表相关的属性 name 。例如:130mm 宽度
我需要实现的内容如下:
Illuminate\Database\Eloquent\Collection {#4400
all: [
App\Models\Product {
id: 1,
name: "Chrystal Block",
details: "Ships in an exclusive branded box.",
price: 50.00,
has_shipment: 1,
created_at: "2019-05-10 02:15:22",
updated_at: "2019-05-10 02:15:22",
deleted_at: null,
properties: Illuminate\Database\Eloquent\Collection {
all: [
App\Models\ProductProperty {
product_id: 4,
property_id: 1,
value: "130",
unit: "mm",
created_at: "2019-05-11 23:09:35",
updated_at: "2019-05-11 23:09:35",
deleted_at: null,
// ***** This is what I'm looking for (hardcoded here *****
property: Illuminate\Database\Eloquent\Collection {
all: [
App\Models\Property {
id: 1,
name: "width",
created_at: "2019-05-11 23:09:35",
updated_at: "2019-05-11 23:09:35",
]
},
},
App\Models\ProductProperty {
product_id: 4,
property_id: 2,
value: "17",
unit: "mm",
created_at: "2019-05-11 23:09:35",
updated_at: "2019-05-11 23:09:35",
deleted_at: null,
},
App\Models\ProductProperty {
product_id: 4,
property_id: 3,
value: "60",
unit: "mm",
created_at: "2019-05-11 23:09:35",
updated_at: "2019-05-11 23:09:35",
deleted_at: null,
},
App\Models\ProductProperty {
product_id: 4,
property_id: 4,
value: "2.78",
unit: "kg",
created_at: "2019-05-11 23:09:35",
updated_at: "2019-05-11 23:09:35",
deleted_at: null,
},
],
},
},
],
}
在此雄辩的查询中,谢谢您的帮助。
我的表如下,带有示例数据:
表“ 产品”
+--------------+---------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------------+---------------------+------+-----+---------+----------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| name | varchar(191) | NO | | NULL | |
| details | text | NO | | NULL | |
| price | double(8,2) | NO | | NULL | |
| has_shipment | tinyint(1) | NO | | NULL | |
| created_at | timestamp | YES | | NULL | |
| updated_at | timestamp | YES | | NULL | |
| deleted_at | timestamp | YES | | NULL | |
+--------------+---------------------+------+-----+---------+----------------+
值:
(1, 'Chrystal Block', 'Ships in an exclusive branded box.', 50.00, 1, '2019-05-10 02:15:22', '2019-05-10 02:15:22', NULL);
表“ 属性”
+------------+---------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+---------------------+------+-----+---------+----------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| name | varchar(191) | NO | | NULL | |
| created_at | timestamp | YES | | NULL | |
| updated_at | timestamp | YES | | NULL | |
| deleted_at | timestamp | YES | | NULL | |
+------------+---------------------+------+-----+---------+----------------+
值:
(1, 'width', '2019-05-11 22:53:45', '2019-05-11 22:53:45', NULL),
(2, 'height', '2019-05-11 22:53:45', '2019-05-11 22:53:45', NULL),
(3, 'length', '2019-05-11 22:53:45', '2019-05-11 22:53:45', NULL),
(4, 'weight', '2019-05-11 22:53:45', '2019-05-11 22:53:45', NULL);
表' product_properties '
+-------------+---------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------------+---------------------+------+-----+---------+-------+
| product_id | int(10) unsigned | NO | MUL | NULL | |
| property_id | int(10) unsigned | NO | MUL | NULL | |
| value | varchar(191) | NO | | NULL | |
| unit | varchar(191) | NO | | NULL | |
| created_at | timestamp | YES | | NULL | |
| updated_at | timestamp | YES | | NULL | |
| deleted_at | timestamp | YES | | NULL | |
+-------------+---------------------+------+-----+---------+-------+
值:
(1, 1, '130', 'mm', '2019-05-11 23:09:35', '2019-05-11 23:09:35', NULL),
(1, 2, '17', 'mm', '2019-05-11 23:09:35', '2019-05-11 23:09:35', NULL),
(1, 3, '60', 'mm', '2019-05-11 23:09:35', '2019-05-11 23:09:35', NULL),
(1, 4, '2.78', 'kg', '2019-05-11 23:09:35', '2019-05-11 23:09:35', NULL);
答案 0 :(得分:4)
要获得关系(嵌套关系),您可以尝试
$product->where('id', 1)->with('properties.property')->get();
我非常喜欢解释in this answer,因为它很清楚。