Posts Learn Components Snippets Categories Tags About
/

Laravel Eloquent, select only rows where the relation exists

Get to know how to select rows only when the relationship exists on Laravel

7 months ago

7 mins read

272 views

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();
new

PostSrc Code Snippets

Learn new snippets today, level up your Laravel, Alpine JS, Vue JS, Tailwind CSS skills and more.

Learn New Snippets

Authored by

PostSrc poster

PostSrc

Full stack developer mainly focusing on the front end side of the web. The main author of PostSrc and loves writing tutorial and guides online.

new

PostSrc Code Components

Collection of Tailwind CSS components for everyone to use. Browse all of the components that's right for your project.

View Components