Eloquent 从数据透视表中获取数据

Posted

技术标签:

【中文标题】Eloquent 从数据透视表中获取数据【英文标题】:Eloquent get data from pivot table 【发布时间】:2019-09-29 09:23:09 【问题描述】:

即使在阅读了文档之后,在使用枢轴“product_properties”从“products”表中查询产品时,也很难从相关的“properties”表中获取“property name”。

我的模特:Models\Product::classModels\Property::classModels\ProductProperty::class

如果我运行查询:

$product = new App\Models\Product();
$product->where('id', 1)->with('properties')->get();

我得到了完整的 product 对象以及 properties,这很好用,但我不知道如何获取属性 名称来自properties 表。

总之,我需要显示与product_properties 表相关的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,
             ,
           ],
         ,
       ,
     ],
   

在此先感谢您对此 Eloquent 查询的任何帮助。

我的表格如下,带有示例数据:

表格'产品'

+--------------+---------------------+------+-----+---------+----------------+
| 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);

【问题讨论】:

foreach($data as $D) foreach($D->ProductProperty as $any) $any->name; // 或者获取数组中的第一个 $D->ProductProperty->frist()->name ; // 如果要将数组转换为文本 $D->ProductProperty->implode('name ' , ',') 这看起来不像我要找的。我想在relations 中的Collection 中获取属性名称 你可以试试$product->where('id', 1)->with('properties.property')->get();。 ProductProperties 应该在其类中具有属性关系,如 $this->belongsTo(Property::class); Check this answer 以获得更清晰的示例 太棒了!快乐编码 :) (请注意,在您的场景中,belongsTo 可能比 hasOne 更适合,因为 Product 属性实际上属于属性) @McRui 他们的行为似乎相似,但并非 100%。看看this,过几天可能会让你头疼:p 【参考方案1】:

为了获得关系的关系(嵌套关系),你可以尝试

$product->where('id', 1)->with('properties.property')->get();

我真的很喜欢in this answer 的清晰解释。

【讨论】:

以上是关于Eloquent 从数据透视表中获取数据的主要内容,如果未能解决你的问题,请参考以下文章

如何使用 Eloquent 在数据透视表中插入多行?

Lumen/Laravel Eloquent - 按数据透视表中的属性过滤

如何从数据透视表中获取数据?

Laravel Eloquent 根据数据透视表字段条件获取多对多关系

Eloquent Query:所有具有相同类别的新闻。数据透视表

如何使用或过滤器从 Excel 中的 OLAP 多维数据集数据透视表中获取数据