Tell me, please, what am I doing wrong? The project has the following tables:
excursion_dates -> id created_at updated_at ticket_types -> id title excursion_date_ticket_type -> id excursion_date_id ticket_type_id count price You must link the first two tables so that ExcursionDate has a lot of TicketType . I was able to add new ones, but data retrieval from existing ones does not work. This code does not work:
ExcursionDate::first()->ticketTypes->first()->title Mistake:
Base table or view not found: 1146 Table 'site.excursion_date_ticket_types' doesn't exist (SQL: select `excursion_date_ticket_types`.*, `excursion_date_ticket_type`.`excursion_date_id` as `pivot_excursion_date_id`, `excursion_date_ticket_type`.`ticket_type_id` as `pivot_ticket_type_id`, `excursion_date_ticket_type`.`price` as `pivot_price`, `excursion_date_ticket_type`.`count` as `pivot_count` from `excursion_date_ticket_types` inner join `excursion_date_ticket_type` on `excursion_date_ticket_types`.`id` = `excursion_date_ticket_type`.`ticket_type_id` where `excursion_date_ticket_type`.`excursion_date_id` = 1) Where could I be wrong?
Models \ ExcursionDateTicketType
class ExcursionDateTicketType extends Model { /** * The attributes that are mass assignable. * * @var array */ protected $fillable = [ 'ticket_type_id', 'excursion_date_id', 'price', 'count' ]; protected $table = 'excursion_date_ticket_type'; /** * Many to Many relation * * @return \Illuminate\Database\Eloquent\Relations\belongsToMany */ public function ticketType() { return $this->belongsToMany(TicketType::class, 'excursion_date_ticket_types', 'ticket_type_id', 'excursion_date_id'); } /** * One to Many relation * * @return \Illuminate\Database\Eloquent\Relations\BelongsTo */ public function excursionDate() { return $this->belongsToMany(excursionDate::class, 'excursion_date_ticket_types', 'excursion_date_id', 'ticket_type_id'); } } Models \ ExcursionDate
class ExcursionDate extends Model { /** * The attributes that are mass assignable. * * @var array */ protected $fillable = [ 'tour_date_start', 'tour_date_finish', 'sell_date_start', 'sell_date_finish', 'booking_date_start', 'booking_date_finish', 'address', 'warranty', 'special', 'discount', 'hot', 'payspot', 'booking' ]; protected $table = 'excursion_dates'; /** * Many to Many relation * * @return \Illuminate\Database\Eloquent\Relations\belongsToMany */ public function ticketTypes() { return $this->belongsToMany(ExcursionDateTicketType::class, 'excursion_date_ticket_type', 'excursion_date_id', 'ticket_type_id')->withPivot('price', 'count'); } } Models \ TicketType
class TicketType extends Model { /** * The attributes that are mass assignable. * * @var array */ protected $fillable = [ 'title', 'description' ]; protected $table = 'ticket_types'; /** * Many to Many relation * * @return \Illuminate\Database\Eloquent\Relations\belongsToMany */ public function excursions() { return $this->belongsToMany(ExcursionDate::class)->withPivot('price', 'count'); } } UPD:
After explicitly specifying $ table in all models, the error changed to:
Not unique table/alias: 'excursion_date_ticket_type' (SQL: select `excursion_date_ticket_type`.*, `excursion_date_ticket_type`.`excursion_date_id` as `pivot_excursion_date_id`, `excursion_date_ticket_type`.`ticket_type_id` as `pivot_ticket_type_id`, `excursion_date_ticket_type`.`price` as `pivot_price`, `excursion_date_ticket_type`.`count` as `pivot_count` from `excursion_date_ticket_type` inner join `excursion_date_ticket_type` on `excursion_date_ticket_type`.`id` = `excursion_date_ticket_type`.`ticket_type_id` where `excursion_date_ticket_type`.`excursion_date_id` = 1)
pivotmodel, then imagine it too. I pointed out the main problem in the answer - Maxim K