Подскажите, пожалуйста, что я делаю не так. В проекте существуют следующие таблицы:
excursion_dates ->
id
created_at
updated_at
ticket_types ->
id
title
excursion_date_ticket_type ->
id
excursion_date_id
ticket_type_id
count
price
Необходимо связать первые две таблицы, чтобы у ExcursionDate
было много TicketType
. Добавление новых я смог сделать, но получение данных из существующих не получается. Не работает этот код:
ExcursionDate::first()->ticketTypes->first()->title
Ошибка:
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)
Где я мог ошибиться?
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:
После явного указания $table во всех моделях ошибка поменялась на:
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)
pivot
модель, то представь еще и ее. Основную проблему я указал в ответе