djt
djt

Reputation: 7535

Laravel - How to write this in Eloquent?

Lets say I have a MessageThread which has many Messages. And each Message is associated with one User.

How would i get all MessageThread objects containing at least one Message that is associated with a particular User?

Upvotes: 2

Views: 37

Answers (1)

Joseph Silber
Joseph Silber

Reputation: 219938

Use two whereHas constraints:

$threads = MessageThread::whereHas('messages', function ($query) {
    $query->whereHas('user', function ($query) {
        $query->where('user_id', 1);
    });
})->get();

Upvotes: 1

Related Questions