我有一張門票表。主要門票表hasOne客戶。客戶表屬於用戶表。如何在Laravel中查詢與hasOne關係的第三個表?
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-> customer-> first_name。
錯誤:SQLSTATE [42S22]:柱未發現:在1054未知列 'users.ticket_id' 'where子句'(SQL:。SELECT * FROM
users
其中users
deleted_at
爲空和users
ticket_id
= 1和users
。ticket_id
不爲空極限1)
任何幫助理解。
謝謝。你是天才! –
工程就像一個魅力。 –