user137
user137

Reputation: 759

Laravel hasManyThrough returns empty result

I have a 3 tables: Chats, Messages, Invoices

enter image description here

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

Answers (1)

Mathieu Ferre
Mathieu Ferre

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

Related Questions