KarChung0930's avatar

Check if table is empty

Is there a method that can check if the table is empty? For now, I'm using:

use App\Models\Info;

is_null(Info::first());

I'm looking for the most effective and easy way like Info::isTableEmpty(), clearly there is no such method for now?

0 likes
11 replies
Shivamyadav's avatar

You can try this..

$infos = Info::all();
dd($infos->isEmpty()) // it returns TRUE if  info table is empty otherwise  return FALSE
1 like
KarChung0930's avatar

@Shivamyadav Hi, thanks for responding! I think this will work but it's not the most effective way because assuming the table have millions of records in it?

tykus's avatar

A count query might be slightly quicker:

Info::count() === 0
1 like
KarChung0930's avatar

@tykus Hello, how are you? Can you explain why the count method might be slightly quicker? I think it is similar to the all method which might not effective if the table has a large number of rows/data?

Whereas is_null(Info::first()) only get the first result and check if the result existed. At the moment, I also found another answer which is Info::exists(). In your opinion, which one is the most effective one?

tykus's avatar

@KarChung0930 Info::all() will fetch data to hydrate Model instances in memory that ultimately are not used. The count fetches an integer value only.

KarChung0930's avatar

@tykus I'm not sure how the count(*) from table works and I believe it is how the count method basically doing. My concern is that even the count method only fetches an integer, but it go through every row for counting.

This is the reason I'm avoiding to use it unless I know how it really work behind the scene. How would you rate the below methods from 1 to 4:

  • is_null(Info::first())
  • Info::all()->isEmpty()
  • Info::count() === 0
  • Info::exists()
tykus's avatar

@KarChung0930

  • Info::first() fetches an entire record (potentially) and hydrates a model unnecessarily
select * from `infos` limit 1
  • Info::all() fetches *all records and makes a Collection of Info instances unnecessarily
select * from `infos`
  • Info::count() fetches an integer count of all rows in the table - nothing is unnecessarily hydrated
"select count(*) as aggregate from `infos`"
  • Info::exists() fetches a boolean determining if there are any records amongst all of the records - nothing is unnecessarily hydrated
select exists(select * from `users`) as `exists`

For me, the count query is simplest, but there is not much in it compared with the exists query; neither produces unnecessary effort on the database or application server

ajgori's avatar

use App\Models\Info;

$isEmpty = Info::count() === 0;

1 like
Jsanwo64's avatar
Jsanwo64
Best Answer
Level 11

If the table contains millions of rows, using Info::count() to determine whether it is empty may not be the most efficient technique because it counts all rows in the table, which can be resource-intensive.

Instead, you can use a more streamlined query to determine whether the table contains any entries without retrieving all of the data. One approach to accomplish this is to use the exists() method, which checks to see if any records meet the specified criteria. Here's an example.

if (!Info::exists()) {
    // Table is empty
} else {
    // Table has data
}
1 like

Please or to participate in this conversation.