【问题标题】:Laravel belongsToMany.belongsTo nested relationship query where child column = parent idLaravel belongsToMany.belongsTo 嵌套关系查询 where child column = parent id
【发布时间】:2020-01-20 18:56:29
【问题描述】:

我有两个模型(和具有 Laravel 命名约定的数据库表),我正在尝试使用 EquipmentEquipment.collection_meters 预先加载 Collections,其中 collection_meter 等于 Collection.id

如何访问子关系$query->with中的collections.id

如何访问子关系$query->with中的equipment.pivot.collection_id

这是我的模型...

class Collection extends Model {
    public function equipment() {
        return $this->belongsToMany(Equipment::class, 'collection_equipment', 'collection_id', 'equipment_id')->withTimestamps();
    }
}

class CollectionMeter extends Model {
    public function equipment() {
        return $this->belongsTo(Equipment::class); 
    }
}

class Equipment extends Model {
    public function collection_meters() {
        return $this->hasMany(CollectionMeter::class)->latest(); // ->currentStatus('active')
    }
}

equipment_id 列在collection_meters 表中

这是我尝试过的代码,但我似乎无法在子关系查询中获得Collection.id。如果我忽略了孩子$query->where,我会得到ALLequipment.equipment_meters

$collections = Collection::with([
    'equipment',
    'equipment.collection_meters' => function($query) {
        $query->where('collection_meters.collection_id', '=', 'collections.id');
    }
])
->get();

我已经尝试用这些代替'collections.id',结果相同 'equipment.pivot.collection_id' 'equipment.collection_id'

我确实在[equipment] => Array 中看到了这个,我可以通过某种方式访问​​$query->where 中的[collection_id] => 2 吗?

[pivot] => Array
    (
        [collection_id] => 2
        [equipment_id] => 1
        [created_at] => 2019-09-17 00:17:00
        [updated_at] => 2019-09-17 00:17:00
    )

没有$query->with 的结果([collection_meters] 有 ALL)

Array
(
    [0] => Array
        (
            [id] => 2
            [name] => Name of Collection
            [operator_id] => 1
            [location_id] => 4
            [account_id] => 1
            [date_time] => 2019-09-16 04:41:26
            [reconciliation_id] => 
            [created_by_id] => 1
            [updated_by_id] => 1
            [created_at] => 2019-09-16 23:44:43
            [updated_at] => 2019-09-18 17:32:53
            [equipment] => Array
                (
                    [0] => Array
                        (
                            [id] => 1
                            [name] => Name of Equipment
                            [ims_identifier] => AFA-64
                            [operator_identifier] => 073076-01
                            [equipment_model_id] => 1
                            [operator_id] => 1
                            [location_id] => 4
                            [created_by_id] => 1
                            [updated_by_id] => 1
                            [created_at] => 2019-07-17 13:17:28
                            [updated_at] => 2019-08-14 00:04:07
                            [pivot] => Array
                                (
                                    [collection_id] => 2
                                    [equipment_id] => 1
                                    [created_at] => 2019-09-17 00:17:00
                                    [updated_at] => 2019-09-17 00:17:00
                                )

                            [collection_meters] => Array
                                (
                                    [0] => Array
                                        (
                                            [id] => 17
                                            [equipment_meter_id] => 1
                                            [value] => 0.25
                                            [gross] => 25.00
                                            [refund] => 2.00
                                            [test] => 2.00
                                            [reading_start] => 72985
                                            [reading_end] => 73085
                                            [collection_id] => 3
                                            [equipment_id] => 1
                                            [operator_id] => 1
                                            [location_id] => 4
                                            [account_id] => 1
                                            [created_by_id] => 1
                                            [updated_by_id] => 1
                                            [created_at] => 2019-09-17 18:23:41
                                            [updated_at] => 2019-09-17 22:56:50
                                        )

                                    [1] => Array
                                        (
                                            [id] => 9
                                            [equipment_meter_id] => 1
                                            [value] => 0.25
                                            [gross] => 24.00
                                            [refund] => 2.00
                                            [test] => 1.00
                                            [reading_start] => 72885
                                            [reading_end] => 72985
                                            [collection_id] => 2
                                            [equipment_id] => 1
                                            [operator_id] => 1
                                            [location_id] => 4
                                            [account_id] => 1
                                            [created_by_id] => 1
                                            [updated_by_id] => 1
                                            [created_at] => 2019-09-17 00:17:00
                                            [updated_at] => 2019-09-18 17:32:53
                                        )

                                    [2] => Array
                                        (
                                            [id] => 1
                                            [equipment_meter_id] => 1
                                            [value] => 0.25
                                            [gross] => 282.50
                                            [refund] => 3.26
                                            [test] => 0.00
                                            [reading_start] => 71755
                                            [reading_end] => 72885
                                            [collection_id] => 1
                                            [equipment_id] => 1
                                            [operator_id] => 1
                                            [location_id] => 4
                                            [account_id] => 1
                                            [created_by_id] => 1
                                            [updated_by_id] => 1
                                            [created_at] => 2019-09-11 22:38:31
                                            [updated_at] => 2019-09-11 22:38:31
                                        )

                                )

                        )

$query->with 的结果([collection_meters] 为空)

Array
(
    [0] => Array
        (
            [id] => 2
            [name] => Name of Collection
            [operator_id] => 1
            [location_id] => 4
            [account_id] => 1
            [date_time] => 2019-09-16 04:41:26
            [reconciliation_id] => 
            [created_by_id] => 1
            [updated_by_id] => 1
            [created_at] => 2019-09-16 23:44:43
            [updated_at] => 2019-09-18 17:32:53
            [equipment] => Array
                (
                    [0] => Array
                        (
                            [id] => 1
                            [name] => Name of Equipment
                            [ims_identifier] => AFA-64
                            [operator_identifier] => 073076-01
                            [equipment_model_id] => 1
                            [operator_id] => 1
                            [location_id] => 4
                            [created_by_id] => 1
                            [updated_by_id] => 1
                            [created_at] => 2019-07-17 13:17:28
                            [updated_at] => 2019-08-14 00:04:07
                            [pivot] => Array
                                (
                                    [collection_id] => 2
                                    [equipment_id] => 1
                                    [created_at] => 2019-09-17 00:17:00
                                    [updated_at] => 2019-09-17 00:17:00
                                )

                            [collection_meters] => Array
                                (
                                )

                        ) ...

预期结果([collection_meters] 具有 [id] => 2 等于 [collection_id] => 2

Array
(
    [0] => Array
        (
            [id] => 2
            [name] => Name of Collection
            [operator_id] => 1
            [location_id] => 4
            [account_id] => 1
            [date_time] => 2019-09-16 04:41:26
            [reconciliation_id] => 
            [created_by_id] => 1
            [updated_by_id] => 1
            [created_at] => 2019-09-16 23:44:43
            [updated_at] => 2019-09-18 17:32:53
            [equipment] => Array
                (
                    [0] => Array
                        (
                            [id] => 1
                            [name] => Name of Equipment
                            [ims_identifier] => AFA-64
                            [operator_identifier] => 073076-01
                            [equipment_model_id] => 1
                            [operator_id] => 1
                            [location_id] => 4
                            [created_by_id] => 1
                            [updated_by_id] => 1
                            [created_at] => 2019-07-17 13:17:28
                            [updated_at] => 2019-08-14 00:04:07
                            [pivot] => Array
                                (
                                    [collection_id] => 2
                                    [equipment_id] => 1
                                    [created_at] => 2019-09-17 00:17:00
                                    [updated_at] => 2019-09-17 00:17:00
                                )

                            [collection_meters] => Array
                                (
                                    [0] => Array
                                        (
                                            [id] => 9
                                            [equipment_meter_id] => 1
                                            [value] => 0.25
                                            [gross] => 24.00
                                            [refund] => 2.00
                                            [test] => 1.00
                                            [reading_start] => 72885
                                            [reading_end] => 72985
                                            [collection_id] => 2
                                            [equipment_id] => 1
                                            [operator_id] => 1
                                            [location_id] => 4
                                            [account_id] => 1
                                            [created_by_id] => 1
                                            [updated_by_id] => 1
                                            [created_at] => 2019-09-17 00:17:00
                                            [updated_at] => 2019-09-18 17:32:53
                                        )

                                )

                        )

【问题讨论】:

    标签: laravel eloquent relationship has-and-belongs-to-many belongs-to


    【解决方案1】:

    你使用什么样的关系? “多对多”还是“一对多”?如果您有三个表,其中一个是“pivot”表,则必须在两个模型中将关系定义为 belongsToMany:

    class Collection extends Model {
    
        public function equipments() {
            return $this->belongsToMany(Equipment::class, 'collection_equipment', 
            'collection_id', 'equipment_id')->withTimestamps();
        }
    }
    

    数据透视表的名称是什么?收集设备?你的结构怎么样?

    collections => collection_equipment

    class Equipment extends Model {
    
        public function collections() {
           return $this->belongsToMany(Collection::class, 'collection_equipment', 
           'equipment_id', 'collection_id')->withTimestamps();
        }
    }
    

    collection_meters 列在哪里?在装备表里?

    那么,如果要获取嵌套表信息,只需:

    $collections = Collection::with('equipments')->get();
    

    但有时您想使用枢轴模型获取信息:

    class CollectionEquipment extends Model {
    
        protected $table = 'collection_equipment';
    
        public function collection() {
           return $this->belongsTo(Collection::class, 'collection_id');
        }
    
        public function equipment() {
           return $this->belongsTo(Equipment::class, 'equipment_id');
        }
    
    }
    

    那么,只需:

    $collections = CollectionEquipment::with(['equipment', 'collection'])
                   ->get();
    

    由于您已经在所有模型类中定义了 collection_id 和 equipment_id,因此您不需要像使用“join”那样在查询中映射它们

    【讨论】:

    • 我已经编辑了我的问题...抱歉,我忘记了Equipment 关系...public function collection_meters() { return $this->hasMany(CollectionMeter::class)->latest(); }
    • 那么,您是否在设备和 CollectionMeter 之间存在“一对多”关系,而在集合和设备之间存在“一对多”关系?如果是这样,我将编辑我的答案
    • 是的,@alexandre-barbosa,这正是我所拥有的……one to manyequipmentCollectionMeter 之间的关系以及 collectionequipment 之间的另一个 one to many 关系
    • 您可以从我发布的Results (toArray()'s) 中看到,我可以获得所有collection_meters 或没有collection_meters,但不是唯一与@ 匹配的987654337@(根)
    • 我需要这个……在从上面急切加载的场景中……WHERE collection_meters.equipment_id in (1, 5, 9, 10, 11, 12, 16) AND collection_meters.collection_id = collections.id
    猜你喜欢
    • 2019-03-30
    • 1970-01-01
    • 1970-01-01
    • 2023-04-05
    • 2020-04-14
    • 1970-01-01
    • 2021-05-29
    • 2021-11-14
    • 1970-01-01
    相关资源
    最近更新 更多