If you have a table and there is no data in the table, and you want to show a default message like “No Data Found” to show users that data is not available in the database.
Let’s see an example, How to show a message if data is null in laravel when we have no data in a variable when we listed data in foreach loop.
I have a view file list.blade.php In this file, I listed all the packages in the blade file, here I applied a Condition, where I display an image if we have no data in our package table.
Write a Condition :
@if ($package->count() > 0 )
@if(isset($package) && !empty($package))
@foreach($package as $key => $pkglist)
<div class="row">
<div class="col-sm-4 img-container">
<h2>{{ $pkglist->name }}</h2>
<p>{{ $pkglist->price }}</p>
<p>{{ $pkglist->description }}</p>
</div>
@endforeach
@endif
@else
<img src="{{asset('frontend_assets/img/package-not-found.png')}}">
<div class="col-md-3">
<a href="{{ URL::previous() }}" class="btn btn-block btn-info btn-sm">Go Back</a>
</div>
@endif
</div>
Output :

So in this article, we have learned, How to show a message if data is null in laravel.