Cover Image for Check If Array is Empty in Blade using Laravel
268 views

Check If Array is Empty in Blade using Laravel

The Laravel Blade, you can easily check if an array is empty or not using the @empty directive. Here’s how you can do it:

PHP
@if(empty($yourArray))
    <p>The array is empty.</p>
@else
    <p>The array is not empty.</p>
@endif

Replace $yourArray with the name of the array you want to check. The @empty directive checks if the variable is empty, which includes checking if it’s an empty array, null, or has a count of zero elements.

Additionally, you can also use the @unless directive to achieve the same result:

PHP
@unless($yourArray)
    <p>The array is empty.</p>
@else
    <p>The array is not empty.</p>
@endunless

Both approaches are valid and achieve the same result of checking whether an array is empty or not in Laravel Blade.

YOU MAY ALSO LIKE...

The Tech Thunder

The Tech Thunder

The Tech Thunder


COMMENTS