McR*_*Rui 1 pivot-table relationship laravel eloquent
即使在阅读文档后,也很难从相关的“属性”表中获取“属性名称”,同时又要从“产品”表中查询具有枢轴“ product_properties”的产品。
我的模型:Models\Product::class,Models\Property::class,Models\ProductProperty::class,
如果我运行查询:
$product = new App\Models\Product();
$product->where('id', 1)->with('properties')->get();
Run Code Online (Sandbox Code Playgroud)
我获得了完整的产品对象以及属性,并且效果很好,但是我不知道是如何从表中获取属性名称的properties。
总之,我需要显示与表相关的表中的属性名称。例如:130mm 宽度propertiesproduct_properties
我需要实现的是以下内容:
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,
},
],
},
},
],
}
Run Code Online (Sandbox Code Playgroud)
在此感谢您对口才查询的任何帮助。
我的表如下,带有示例数据:
表“ 产品 ”
+--------------+---------------------+------+-----+---------+----------------+
| 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 | |
+--------------+---------------------+------+-----+---------+----------------+
Run Code Online (Sandbox Code Playgroud)
值:
(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);
Run Code Online (Sandbox Code Playgroud)
表的“ 属性 ”
+------------+---------------------+------+-----+---------+----------------+
| 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 | |
+------------+---------------------+------+-----+---------+----------------+
Run Code Online (Sandbox Code Playgroud)
值:
(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);
Run Code Online (Sandbox Code Playgroud)
表' 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 | |
+-------------+---------------------+------+-----+---------+-------+
Run Code Online (Sandbox Code Playgroud)
值:
(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);
Run Code Online (Sandbox Code Playgroud)
为了获得关系的关系(嵌套关系),您可以尝试
$product->where('id', 1)->with('properties.property')->get();
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
81 次 |
| 最近记录: |