How to check if a variable has data in laravel blade

How to check if a variable has data in laravel blade

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

How to check if a variable has data in Laravel Blade

A common stumbling block for developers working with Laravel collections and Eloquent queries is handling empty result sets gracefully. You might have code that renders a list of products, but when the query returns zero rows, your view simply displays nothing instead of a helpful "No Product Found" message. This often happens because the logic used to detect an empty collection differs from the logic required to render content inside a foreach loop.

The core issue lies in understanding how Laravel's get() method behaves and how Blade interprets falsy values. When you execute $products = Product::where(...) ->get(), you are not returning a boolean or a simple integer; you are returning an instance of Illuminate\Database\Eloquent\Collection. Even if no records match your query criteria, the Collection object still exists. It is simply empty.

In PHP, an empty collection evaluates to false in a boolean context only if it has zero items. However, the specific pattern often used in Blade templates can be tricky. If you rely on standard truthiness checks without accounting for how Blade handles empty arrays or collections within loops, you might end up skipping your conditional block entirely or rendering an invisible gap in your layout.

The solution involves explicitly checking the count of the collection before attempting to iterate over it. In your controller, the query logic is sound:

$products = Product::where('name', 'like', '%'.$name.'%')->get();
return view('cart.product', compact('products'));

The data passed to the view is a Collection. To handle the empty state correctly in your Blade template, you have two primary approaches depending on how strict you want your logic to be.

Using the Count Method for Explicit Checks

The most robust way to handle this scenario is to check if the collection has any items using the count() method or the isEmpty() helper. This approach separates the decision of "what to show" from the data rendering process clearly.

@php
    $hasProducts = $products->isNotEmpty();
@endphp

@if($hasProducts)
    @foreach($products as $product)
        <!-- Render product HTML here -->
    @endforeach
@else
    <div class="alert alert-warning">
        <strong>Sorry!</strong> No Product Found.
    </div>
@endif

By moving the check to a boolean variable or using isNotEmpty() directly in the condition, you ensure that the foreach loop only executes when data exists. If the query returns an empty collection, $products->isNotEmpty() returns false, triggering the "No Product Found" alert immediately. This prevents the browser from rendering an invisible list where the <ul> or <div> tags are present but contain no children, which can sometimes cause layout issues with CSS flexbox or grid systems.

The Truthiness of Empty Collections

It is important to note that while if($products) in Blade usually works because an empty collection is technically falsy in PHP, relying on this behavior inside a template can be brittle if the variable is not initialized correctly or if you are mixing arrays and collections. Explicitly checking the count or emptiness state makes your intent clearer to future maintainers.

If you prefer to keep the logic inline without extra variables, you can use:

@if(count($products))
    @foreach($products as $product)
        <!-- Product HTML -->
    @endforeach
@endif

Here, count() returns 0 for an empty collection, which evaluates to false, effectively hiding the loop content and showing your fallback message.

Handling Null vs. Empty Collections

Your original code snippet used @if(is_null($products)). This check specifically looks for a variable that hasn't been assigned at all or is explicitly set to null. In your controller, $products is always assigned an instance of Illuminate\Database\Eloquent\Collection, even if that collection contains zero items. Therefore, is_null($products) will always return false after the query runs, regardless of whether the database returned rows or not. This is why your "No Data Found" message was never displaying; the condition was technically never met because an empty collection is not null.

To fix this without changing the controller, simply replace the null check with an emptiness check:

@if($products->isEmpty())
    <div class="alert alert-warning">
        <strong>Sorry!</strong> No Product Found.
    </div>
@else
    @foreach($products as $product)
        <!-- Render product -->
    @endforeach
@endif

This adjustment ensures that your view correctly distinguishes between a missing variable and a query that simply returned no results, providing a consistent user experience regardless of the search input.

Our network