Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding out if a table is empty in laravel

Tags:

laravel

I'm trying to find out if my table is empty but I get this error

Call to undefined function App\Http\Controllers\isEmpty()

I did check the question that had a similar title to mine but that didn't have what I needed.

Here is my code

$orders = Order::all();

if(isEmpty($orders))
{
    echo "Im empty";
    die();
 }else{
    echo "im not empty";
     die();
 }
like image 756
Nikki Avatar asked Oct 28 '25 17:10

Nikki


1 Answers

You should use Model::exists(). It returns true/false and runs a select count(*) from table where query under the hood.

if (Order::exists()) {
    echo "im not empty";
    die();
} else {
    echo "Im empty";
    die();
}
like image 145
Cameron Wilby Avatar answered Oct 30 '25 14:10

Cameron Wilby