This code:
$users = Users::with(['posts' => function($post) use ($request) {
$post->where('votes', '>', 100);
}])->get();
dd($users->toArray());
Return output like this:
0 => [
"user_id" => 1
"posts" => []
]
What can I do to return an empty array of users?
I tried to do like this:
$users = Users::with(['posts'])
->where('posts.votes', '>', 100)->get();
dd($users->toArray());
But got an error:
Undefined table: 7 ERROR: issing FROM-clause entry for table "posts"
My Answer:
> Yes, you can use the `where` condition after calling a relation with the `with` method in Laravel.
For example, you can do something like this:
php
$users = Users::with(['posts'])->where('posts.status', 'published')->get();
This code will load the `posts` relation for each user and then filter the users based on the condition that the post status is 'published'.
Rate this post
3 of 5 based on 6670 votesComments