Reputation: 759
I have a 3 tables: Chats, Messages, Invoices
Before, I got chat invoices like this:
//ChatModel:
public function invoices()
{
return $this->hasMany(Invoice::class);
}
But now I need to get all chat invoices which present in chat messages of specific chat. Should I use hasManyThrough for this? Unfortunately, the next example returns an empty result
//ChatModel:
public function invoices()
{
return $this->hasManyThrough(Invoice::class, Message::class, 'chat_id', 'id');
}
Upvotes: 0
Views: 73
Reputation: 4412
As you have both chat_id and invoice_id in your messages table you should just your messages table as a pivot table !
public function invoices()
{
return $this->belongsToMany(Invoice::class, 'messages');
}
Upvotes: 1