DB Models and EloquentTIP 53 / 100
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.
public function index()
{
if (\App\Models\User::exists()) {
// Returns true when the table contains at least one row.
}
if (\App\Models\User::count()) {
// Returns the total number of rows in the table.
}
}