Please guide me,
How can I get all users in Laravel, who has posted at least single post. And skip who has not posted any posts.
I am trying this. But it is getting All users.
$users = User::with('post')->get();
Try has()
method for Eloquent models -> https://laravel.com/docs/5.4/eloquent-relationships
$users = User::with('post')->has('post')->get();
You can take users with active posts using whereHas()
. Remember it. :)
$users = User::with('post')->whereHas('post', function ($query) {
$query->where('is_active', '=', true);
})->get();
You can use :
User::with('post')->has('post')->get();
or whereHas
(from laravel doc)
// Retrieve all posts with at least one comment containing words like foo%
$posts = Post::whereHas('comments', function ($query) {
$query->where('content', 'like', 'foo%');
})->get();
Try this:
$users = User::has('post')->get();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With