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