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) 
  • Can you imagine both models? - Maxim K
  • Or if you use your pivot model, then imagine it too. I pointed out the main problem in the answer - Maxim K
  • @Maxik Added models - Eddir 5:58

1 answer 1

The problem is that you are trying to link the ExcursionDate model with the ExcursionDateTicketType model. If you connected with the TicketType model, then there would be no problems.

But if you have a TicketType model (based on the name of the ticket_types table) called ExcursionDateTicketType , then you should either rename it to TicketType or explicitly set the property protected $table = 'ticket_types';

upd: In ExcursionDate replace

 /** * Many to Many relation * * @return \Illuminate\Database\Eloquent\Relations\belongsToMany */ public function ticketTypes() { return $this->belongsToMany(TicketType::class, 'excursion_date_ticket_type', 'excursion_date_id', 'ticket_type_id')->withPivot('price', 'count'); } 

Plus, you can not use the ExcursionDateTicketType model, you probably don’t need it in principle. If you think you need it, review the code again. Additional data in the pivot table ( count , price ) can be added / updated from the relationship methods

  • Specifying the table in all models partially helped, but another error appeared. I need ExcursionDateTicketType due to the possibility of specifying price and count for a specific ExcursionDate with TicketType. - Eddir