嗨,我有两个与这个bug相关的模型;事件和TimeSlot。他们彼此之间有BelongsToMany关系。
我创建了一个索引查询,以便只检索与可用事件关联的时隙。
问题是,如果我使用return $query->whereIn('id',$time_slot_ids);
,我会在事件条目上收到SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'id' in where clause is ambiguous
的消息。
如果我改为使用return $query->whereIn('time_slot_id',$time_slot_ids);
,则查询将在Event上运行并正确返回附加的时隙。然而,这反过来导致时间槽页面上的错误消息读取SQLSTATE[42S22]: Column not found: 1054 Unknown column 'time_slot_id' in 'where clause'
。
我的完整代码块;
/**
* Build an "index" query for the given resource.
*
* @param \Laravel\Nova\Http\Requests\NovaRequest $request
* @param \Illuminate\Database\Eloquent\Builder $query
* @return \Illuminate\Database\Eloquent\Builder
*/
public static function indexQuery(NovaRequest $request, $query)
{
if(auth()->user()->userLevel->level == 'Group') {
$time_slot_ids = [];
foreach(auth()->user()->group->events as $event) {
foreach($event->timeSlots as $timeSlot) {
$time_slot_ids[] = $timeSlot->id;
}
}
return $query->whereIn('time_slot_id',$time_slot_ids);
}
}
创建User,UserLevel,Group,Event和Time Slot模型以及随附的Nova资源。
用户属于用户级别。用户属于某个组。用户属于许多事件。用户属于许多时隙。
用户级别有很多用户。
集团有很多用户。集团有很多活动。
活动属于某个组。事件属于许多用户。事件属于许多时段。
时间段属于许多事件。 Time Slot属于很多用户。
尝试创建索引查询,其中仅返回附加到经过身份验证的用户的组事件的时隙。
难道我做错了什么?
这是我收到第一个错误之前执行的查询的完整列表;
一次查询多个表时,必须指定公用列名的表名。所以在查询id时,请使用:
return $query->whereIn('time_slots.id',$time_slot_ids);