- Laravel Version: 5.8
- Nova Version: 2.0.0
- PHP Version: 7.2.1
- Operating System and Version: Max OSX 10.13.6
- Browser type and version: Firefox 66.0.3
Description:
Hi, I have 2 models relevant to this bug; Event and TimeSlot. They have a BelongsToMany relationship with each other.
I've created an index query so that only the time slots associated with the available events are retrieved.
The problem is that if I use return $query->whereIn('id',$time_slot_ids);
I get a message on the Event entry that says SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'id' in where clause is ambiguous
.
If I instead use return $query->whereIn('time_slot_id',$time_slot_ids);
the query works on the Event and returns the attached time slots correctly. However this in turn leads to an error message on the Time Slots page which reads SQLSTATE[42S22]: Column not found: 1054 Unknown column 'time_slot_id' in 'where clause'
.
My full block of code;
/**
* 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);
}
}
Steps To Reproduce:
Create a User, UserLevel, Group, Event and Time Slot model and accompanying Nova resource.
User belongs to a User Level. User belongs to a Group. User belongs to many Events. User belongs to many Time Slots.
User Level has many Users.
Group has many Users. Group has many Events.
Event belongs to a Group. Event belongs to many Users. Event belongs to many Time Slots.
Time Slot belongs to many Events. Time Slot belongs to many Users.
Attempt to create an index query where only the time slots attached to the authenticated user's group's events are returned.
Am I doing something wrong?
Here's the full list of queries executed before I get the first error;
and `id` in (...)
, and the second it's trying to find a time_slot_id in the tabletime_slots
. - aynber