How to display 0 if no record in laravel?

Member

by addison , in category: PHP Frameworks , a year ago

How to display 0 if no record in laravel?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by elisha_langworth , a year 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.

Related Threads:

How to display record count in laravel?
How to return 0 instead of null in laravel?
How to check if the record is exist in laravel?
How to update record from an array in laravel?
How to check current date record in laravel?
How to update multiple record using laravel?