There are two common ways of determining if a table is empty in Laravel

There are two common ways of determining if a table is empty in Laravel. Calling exists() or count() directly on the model!

One returns a strict true/false boolean, the other returns an integer which you can use as a falsy in conditionals.

1public function index()
2{
3 if (\App\Models\User::exists()) {
4 // returns boolean true or false if the table has any saved rows
5 }
6 
7 if (\App\Models\User::count()) {
8 // returns the count of rows in the table
9 }
10}

Tip given by @aschmelyun

Like our articles?

Become a Premium Member for $129/year or $29/month
What else you will get:
  • 22 courses (477 lessons, total 38 h 20 min)
  • 2 long-form tutorials (one new every week)
  • access to project repositories
  • access to private Discord