Eager Download multiple filter relationships to communicate with Eloquent ORM

I have the following code that does not work:

$threads = MessageThread::with('last_message', 'thread_visibility') ->where('message_thread_visibility.user_id', Auth::user()->id)->get(); 

What is the best way to add a "where" clause to an eagerly loaded eloquent query in Laravel?

+4
source share
1 answer
 $threads = MessageThread::with(array( 'last_message', 'thread_visibility' => function($query) { $query->where('user_id', 2); }))->get(); 

The above seems to work.

+6
source

Source: https://habr.com/ru/post/1488063/


All Articles