I have three models with the following setup:
- Bookings: id, [more fields]
- Slots: id, [more fields]
- Tickets: id, booking_id, slot_id
So in writing: A booking has one or more tickets associated with it. I now want to retrieve the Slots associated with a booking.
So I wanted to leverage a hasManyThrough relationship:
// App\Models\Slot.php
...
public function booking()
{
return $this->hasManyThrough(
'App\Models\Booking',
'App\Models\Ticket'
}
}
...
// App\Models\Ticket.php
...
public function slot()
{
return $this->hasOne('App\Models\Slot');
}
public function booking()
{
return $this->hasOne('App\Models\Booking');
}
...
But somehow this results in a non-unique error:
SQLSTATE[42000]: Syntax error or access violation: 1066 Not unique table/alias: 'bookings' (SQL: select `bookings`.*, `bookings`.`slot_id` as `laravel_through_key` from `bookings` inner join `bookings` on
`bookings`.`id` = `bookings`.`ticket_id` where `bookings`.`slot_id` in (3))
What am I missing here? Am I correct to use a hasManyThrough relationship? It looks like I'm messing up the default keys?