Laravel belongsToMany.belongsTo嵌套关系查询,其中子列=父id
我有两个模型(以及具有Laravel命名约定的数据库表),我正尝试使用Laravel belongsToMany.belongsTo嵌套关系查询,其中子列=父id,laravel,eloquent,relationship,has-and-belongs-to-many,belongs-to,Laravel,Eloquent,Relationship,Has And Belongs To Many,Belongs To,我有两个模型(以及具有Laravel命名约定的数据库表),我正尝试使用设备和设备来加载集合,其中集合等于集合.id 如何访问子关系$query->with中的集合.id? [pivot] => Array ( [collection_id] => 2 [equipment_id] => 1 [created_at] => 2019-09-17 00:17:00 [updated_at] => 2
设备和设备来加载集合
,其中集合
等于集合.id
如何访问子关系$query->with
中的集合.id
?
[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
中的设备.pivot.collection\u id
?
[pivot] => Array
(
[collection_id] => 2
[equipment_id] => 1
[created_at] => 2019-09-17 00:17:00
[updated_at] => 2019-09-17 00:17:00
)
这是我的模型
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')
}
}
设备id
列位于集合仪表
表中
这是我尝试过的代码,但我似乎无法在子关系查询中获取集合.id
。如果我省略子项$query->where
,我将得到设备.设备仪表的所有
$collections = Collection::with([
'equipment',
'equipment.collection_meters' => function($query) {
$query->where('collection_meters.collection_id', '=', 'collections.id');
}
])
->get();
我已经用这些方法代替了'collections.id'
,得到了相同的结果
'device.pivot.collection\u id'
“设备收集id”
我确实在[设备]=>阵列中看到了这一点,我是否可以访问$query->where
中的[集合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\u meters]
有全部)的结果
$query->with
的结果([collection\u meters]
为空)
预期结果([collection\u meters]
具有[id]=>2
等于[collection\u id]=>2
)
你在使用什么样的关系?“多对多”还是“一对多”?如果有三个表,其中一个是“pivot”,则必须在两个模型中定义关系,如下所示:
class Collection extends Model {
public function equipments() {
return $this->belongsToMany(Equipment::class, 'collection_equipment',
'collection_id', 'equipment_id')->withTimestamps();
}
}
透视表的名称是什么?收集设备?你的结构怎么样
收集=>收集设备我已经编辑了我的问题。。。对不起,我忘记了设备
关系public function collection_meters(){return$this->hasMany(CollectionMeter::class)->latest();}
那么,设备和CollectionMeter之间是否存在“一对多”关系,以及集合和设备之间是否存在“一对多”关系?如果是这样,我将编辑我的答案是的,@alexandre barbosa,这正是我所拥有的<代码>一对多
设备
和采集仪
之间的关系,以及采集
和设备
之间的另一个一对多
关系,您可以从我发布的结果中看到(toArray())我可以得到所有的集合\u表
或者没有一个集合\u表
只是不是唯一一个与集合.id
(根)匹配的集合。不知何故,我需要这个。。。在上面迫不及待加载的场景中<代码>其中集合\u仪表.设备\u id位于(1,5,9,10,11,12,16)和集合\u仪表.集合\u id=集合.id
class Collection extends Model {
public function equipments() {
return $this->belongsToMany(Equipment::class, 'collection_equipment',
'collection_id', 'equipment_id')->withTimestamps();
}
}
class Equipment extends Model {
public function collections() {
return $this->belongsToMany(Collection::class, 'collection_equipment',
'equipment_id', 'collection_id')->withTimestamps();
}
}
$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();