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