Laravel Check If Relationship Has Data Query Example

Hello Artisan 

In this tutorial i will explain existence of a relationship in Laravel. For example, imagine you would like to fetch all blog posts that have at least one comment. To do that, you have to pass the name of the relationship to the has and orHas methods:

Laravel check if relationship has data then we will fetch only those data. We can do it using laravel has() eloquent method. See the Example code.

// Retrieve all posts that have at least one comment...
$posts = App\Models\Post::has('comments')->get();

 

Read also : Laravel whereHas and whereDoesntHave Query Example

Hope it will help you.

 

author-image
Facebook Github
A web enthusiastic, a self-motivated full-stack software engineer from Dhaka, Bangladesh with experience in developing applications using Laravel , React and Vue js