У меня есть билеты стол. Главный билет таблица естьOne клиент. Таблица клиентов относится к таблице пользователей.Как запросить третью таблицу с отношениями hasOne в Laravel?
Ticket.php
public function user()
{
return $this->hasOne('App\Models\User');
}
/**
* Get the TicketStatus record associated with the Ticket.
* One ticket has only one status
*/
public function ticket_status(){
return $this->hasOne('App\TicketStatus','id','ticket_status_id');
}
/**
* Get the Customer record associated with the Ticket.
* One ticket has only one customer
*/
public function customer()
{
return $this->hasOne('App\Models\Customer', 'id', 'customer_id');
}
Customer.php
public function user()
{
return $this->belongsTo(User::class);
}
/**
* Get the Ticket that owns the Customer.
*/
public function ticket()
{
return $this->belongsTo('App\Ticket');
}
User.php
index.blade.php
@foreach($tickets as $item)
<tr>
<td>{{ $item->customer->first_name }}</td>
</tr>
@endforeach
Основная таблица билетов. Он имеет foreign_key для пользователей и клиентов. Таблица Customers имеет внешний ключ для пользователя, который содержит first_name и last_name.
Проблема: Я хочу, чтобы получить доступ к $ item-> на клиента> first_name.
Ошибка: SQLSTATE [42S22]: Column не найдено: 1054 Неизвестный столбец 'users.ticket_id' в 'где предложение' (SQL:.. * Выберите из
users
гдеusers
deleted_at
является нулевым иusers
ticket_id
= 1 иusers
.ticket_id
не нулевой предел 1)
Любая помощь приветствуется.
спасибо. Ты гений !!! –
Работает как шарм. –