How to display 0 if no record in laravel?

Member

by addison , in category: PHP Frameworks , 13 days ago

How to display 0 if no record in laravel?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by elisha_langworth , 12 days ago

@addison 

To display 0 if there are no records in Laravel, you can use the count() method to check if there are any records in the database. If there are no records, you can then display 0. Here's an example:

1
2
3
4
5
6
7
$recordsCount = Model::count();

if($recordsCount == 0){
    $recordsCount = 0;
}

return view('your-view', compact('recordsCount'));


In your view file, you can display the count like this:

1
<p>Total records: {{ $recordsCount }}</p>


This will display 0 if there are no records in the database.