Home / Tutorials / Laravel Eloquent, select only rows where the relation exists
Laravel Eloquent, select only rows where the relation exists cover

Laravel Eloquent, select only rows where the relation exists

7 mins

4.4K

3 years ago

0 comments

Average

Selecting rows using Laravel Eloquent is fairly straight forward but what some people not aware of is when the table contains a relationship. Imagine having a table called "posts" and the table has a relation of one-to-many "comments" or "likes".

Query Relationship


In order to query the table with conditions that are across relation, we have to use the "has" method of the model. The code below will paginate posts that at least have one comment or likes. If the post has no comments or likes then it won't be queried.
use App\Post;

Post::has('comments')->paginate(16);
Post::has('likes')->paginate(16);
Just like the "where" method, the "has" method can accept the operator, and the total number of values a condition has to be met.
use App\Post;

// Retrieve all posts that have equal or more than 3 likes...
$posts = Post::has('likes', '>=', 3)->get();

// Retrive all post that have at least one comments and the comments have at least one like
$posts = Post::has('comments.likes')->get();

Advance Query Relationship


If you want to query the relation but it contains a fairly complex condition, you can use whereHas then specify the relationship and pass in an anonymous function. You can define any query constrain inside this function and if it's met, the "post" will return only with the relationship defined.
use App\Post;

$posts = Post::whereHas('comments', function (Builder $query) {
    $query->where('content', 'like', 'code%');
})->get();

Inverse of whereHas


For there inverse of "whereHas" you can use "doesntHave" method and the query will only retrieve model that has no "relation".
use App\Post;

$posts = Post::doesntHave('likes')->get();
notion avatar

Ahmad Aziz

Week-end developer currently experimenting with web, mobile, and all things programming.

Support Us

If you like our tutorial, support us by being our Patreon or buy us some coffee ☕️

Related Tutorials

Code Snippets

Collection of Laravel, AlpineJS, VueJS, Tailwind CSS, Flutter snippets and more. New snippets added every day.
View Snippets

Code Components

Collection of Tailwind CSS components for everyone to use. Browse all of the components that's right for your web projects
View Components

Welcome to PostSrc V3

PostSrc Dark Logo

You have to login to favorite this