Laravel Eloquent 关系中的联合模型
Laravel Eloquent union models from relationsships
我有一个用户可以与不同的人开会 relationships.He 可以直接分配给一个有太多关系的会议,他可以有太多关系的用户组,可以与太多人开会很多关系,他可以负责一个一对多关系的会议。
我想为一个用户获取所有会议。我不想合并 collections,因为我希望能够直接在数据库中使用 where
等功能。我的尝试是合并所有会议 ID,然后通过 'whereIn'.
获取会议
$ids = $this->meetings()->select('meetings.id')
->union($this->meetingsFromGroups()->select('meetings.id'))
->union($this->responsibleMeetings()->select('meetings.id'));
return Meeting::whereIn('id', $ids)->get();
当我执行这个时,我得到这个错误:
"SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'union (select `meetings`.`id` from `meetings` inner join `meeting_user_group` on' at line 1 (SQL:...)"
The SQL that is executed:
select *
from `meetings`
where `id` in (
(
select `meetings`.`id`
from `meetings`
inner join `meeting_user` on `meetings`.`id` = `meeting_user`.`meeting_id`
where `meeting_user`.`user_id` = 1
)
union
(
select `meetings`.`id`
from `meetings`
inner join `meeting_user_group` on `meeting_user_group`.`meeting_id` = `meetings`.`id`
inner join `user_groups` on `user_groups`.`id` = `meeting_user_group`.`user_group_id`
inner join `user_user_group` on `user_user_group`.`user_group_id` = `user_groups`.`id`
where `user_user_group`.`user_id` = 1
)
union
(
select `meetings`.`id`
from `meetings`
where `meetings`.`responsible_id` = 1
and `meetings`.`responsible_id` is not null
)
)
关系:
public function responsibleMeetings(): HasMany {
return $this->hasMany(Meeting::class, 'responsible_id');
}
public function meetings(): BelongsToMany {
return $this->belongsToMany(Meeting::class)->withTimestamps();
}
public function meetingsFromGroups(): HasManyDeep {
return $this->hasManyDeepFromRelations($this->userGroups(), (new UserGroup())->meetings());
}
我正在为 meetingsFromGroups
使用软件包 Staudenmeir\EloquentHasManyDeep
。
我不能在整个会议上使用 union
,因为那样我会得到
SQLSTATE[21000]: Cardinality violation: 1222 The used SELECT statements have a different number of columns
因为数据透视字段。
SQL 需要如何更改才能正常工作,我如何使用 Eloquent 进行更改?
默认情况下 whereIn
期望数组对数组项执行 IN()
子句,但在您的情况下,您发送的是联合查询,这就是它抛出错误的原因。
$ids = $this->meetings()->select('meetings.id')
->union($this->meetingsFromGroups()->select('meetings.id'))
->union($this->responsibleMeetings()->select('meetings.id'))
->pluck('id')->all();
return Meeting::whereIn('id', $ids)->get();
另外 whereIn
也接受闭包方法但不确定如何在闭包
中调用这些相关的 $this->meetings()
,$this->meetingsFromGroups()
& $this->responsibleMeetings()
return Meeting::whereIn('id', function($query){
$query->select('...')
...;
})->get();
我有一个用户可以与不同的人开会 relationships.He 可以直接分配给一个有太多关系的会议,他可以有太多关系的用户组,可以与太多人开会很多关系,他可以负责一个一对多关系的会议。
我想为一个用户获取所有会议。我不想合并 collections,因为我希望能够直接在数据库中使用 where
等功能。我的尝试是合并所有会议 ID,然后通过 'whereIn'.
$ids = $this->meetings()->select('meetings.id')
->union($this->meetingsFromGroups()->select('meetings.id'))
->union($this->responsibleMeetings()->select('meetings.id'));
return Meeting::whereIn('id', $ids)->get();
当我执行这个时,我得到这个错误:
"SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'union (select `meetings`.`id` from `meetings` inner join `meeting_user_group` on' at line 1 (SQL:...)" The SQL that is executed:
select *
from `meetings`
where `id` in (
(
select `meetings`.`id`
from `meetings`
inner join `meeting_user` on `meetings`.`id` = `meeting_user`.`meeting_id`
where `meeting_user`.`user_id` = 1
)
union
(
select `meetings`.`id`
from `meetings`
inner join `meeting_user_group` on `meeting_user_group`.`meeting_id` = `meetings`.`id`
inner join `user_groups` on `user_groups`.`id` = `meeting_user_group`.`user_group_id`
inner join `user_user_group` on `user_user_group`.`user_group_id` = `user_groups`.`id`
where `user_user_group`.`user_id` = 1
)
union
(
select `meetings`.`id`
from `meetings`
where `meetings`.`responsible_id` = 1
and `meetings`.`responsible_id` is not null
)
)
关系:
public function responsibleMeetings(): HasMany {
return $this->hasMany(Meeting::class, 'responsible_id');
}
public function meetings(): BelongsToMany {
return $this->belongsToMany(Meeting::class)->withTimestamps();
}
public function meetingsFromGroups(): HasManyDeep {
return $this->hasManyDeepFromRelations($this->userGroups(), (new UserGroup())->meetings());
}
我正在为 meetingsFromGroups
使用软件包 Staudenmeir\EloquentHasManyDeep
。
我不能在整个会议上使用 union
,因为那样我会得到
SQLSTATE[21000]: Cardinality violation: 1222 The used SELECT statements have a different number of columns
因为数据透视字段。
SQL 需要如何更改才能正常工作,我如何使用 Eloquent 进行更改?
默认情况下 whereIn
期望数组对数组项执行 IN()
子句,但在您的情况下,您发送的是联合查询,这就是它抛出错误的原因。
$ids = $this->meetings()->select('meetings.id')
->union($this->meetingsFromGroups()->select('meetings.id'))
->union($this->responsibleMeetings()->select('meetings.id'))
->pluck('id')->all();
return Meeting::whereIn('id', $ids)->get();
另外 whereIn
也接受闭包方法但不确定如何在闭包
$this->meetings()
,$this->meetingsFromGroups()
& $this->responsibleMeetings()
return Meeting::whereIn('id', function($query){
$query->select('...')
...;
})->get();