Laravel check if relation is empty

Stefan Izdrail

Founder & Senior Architect · 2026-06-29

Laravel Company
Title: Efficiently Checking for Empty Relationships in Laravel Apps Introduction In Laravel, you may encounter situations where you need to check if a relationship between objects is empty or not. This can be a bit tricky, especially when using functions like is_null, isset, or != undefined. Thankfully, there are ways to handle this that will make the process much easier for developers. In this article, we'll discuss the best methods of checking if a relationship is empty in Laravel apps while also providing relevant code examples and useful tips. Checking Eloquent Collections A common scenario involves working with database models represented as collections. Here's an example:
$users = User::all();
if ($users->isEmpty()) {
  echo "No users found.";
} else {
  foreach ($users as $user) {
    // Process each user
  }
}
In this case, the isEmpty() method is used to check if a Laravel collection is empty or not. Alternatively, you can use the count() function:
$users = User::all();
if (count($users) === 0) {
  echo "No users found.";
} else {
  foreach ($users as $user) {
    // Process each user
  }
}
Working with Eloquent Relationships Sometimes, you might need to check the relationships of a model. Here's an example:
$posts = Post::with('comments')->get();
foreach ($posts as $post) {
  if (count($post->comments)) {
    echo "This post has comments.";
  } else {
    echo "This post doesn't have any comments yet.";
  }
}
In this example, the get() method is used to load all posts with their comments. The code checks whether each post has received any comments or not by counting them through the count() function. Checking Null Relationships The previous examples worked only when you had collections and relationships loaded; however, there are times where a relationship could result in null values if it hasn't been defined yet or doesn't exist. In this scenario, you need to use the following approach:
$post = Post::where('title', '=', 'My First Post')->first();
if ($post && !$post->comments) {
  echo "This post has no comments yet.";
} else if (!$post) {
  echo "No posts found with the title 'My First Post'.";
}
In this example, if a post with the specified title exists but doesn't have any comments, it will display that message. If there are no posts with that title at all, another message will be shown instead. This helps you handle null relationships effectively. Conclusion Checking for empty relations in your Laravel apps can be done using a variety of methods depending on the scenario and context. By following the tips provided in this article, you should now have a better understanding of how to efficiently check if a relationship is empty or not. Remember, always try different approaches until you find what works best for your particular situation. For more Laravel resources and tutorials, visit https://laravelcompany.com.