Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Slamm's avatar
Level 1

Eloquent find() and count()

Hi all,

I'm experiencing some weird behaviour. I'm selecting a single record from my database using: Model::find(id). Since I have to distinguish if I have a single record or multiple I need to count my records. I'd like to use Builder::count() for this. I call it inside my template as followed: $model->count().

However, here's the part where I ... well ... get crazy.

If I use Model::find(id)->count()or Model::where(condition)->first()->count() I always end up getting 2 as result. On the other hand, if I use count($model) or Model::where(condition)->get()->count() I end up getting the correct result (1), which is what I need.

I'm using Laravel 5.4 btw.

What am I missing? Any hint is appreciated!

Thanks. Patrick.

Edit: changed Laravel 5.8 to Laravel 5.4.

0 likes
10 replies
neeonline's avatar

Hold one, you have multiple records with the same ID?

Nakov's avatar

There is a good answer on this topic here.

Basically when you do a count on the model instance it returns all the results not always 2 as your example, that might be because in your database for the model you have only two rows, but having 50 it will return 50. The proper way is to use the where clause, then you can do just

Model::where('id', 1)->count();
3 likes
JenuelDev's avatar

@Nakov is this good for counting data over a million data? 1,545,890 to be exact.. thanks thanks.

Nakov's avatar

@BroJenuel it depends. This is pretty much like running an SQL statement, so it should be fast, but depends on your where clause. If you are adding multiple checks, your field is not indexed or anything, might be slow. So you should test that.

1 like
Slamm's avatar
Level 1

@NEEONLINE - of course not! For go'd sake... primary keys are unique.

It's just that I tried to be lazy and wanted to reuse the same template multiple times. I do have multiple records but everytime with a different primary key (1,2,3,4, etc.). Depending on the situation I'd like to retrieve my records with Model::find($id) or Model::where(condition).

1 like
Slamm's avatar
Level 1

@NAKOV - I'm totally sorry if I didn't write my question as it's been intended to be asked. I've tried various things already but always end up with > 1 "record" when I'm using $model->count(). To be precise: I get exactly the records as intended (1 or n), but $model->count()returns on a single record 2 (using find() or first()) or the intended amount of records, when using get() or count($model).

As descript in my answer to neeonline, I've tried to distinguish between different cases while reusing templates.

neeonline's avatar

Oh I see,

So you want to use the same query method, for example:

Model::where('id', $id)->count() or Model::where('price', '>', 100)->count();

So, the Model::find(id)->count() is the strange one. That should return just one record since the find uses the unique ID column.

Every time I have issues like this (related to Database), I try to see the query logs:

DB::connection()->enableQueryLog();

Model::find(id)->count();

dd(DB::getQueryLog());

I hope that help you.

Best regards,

2 likes
Slamm's avatar
Level 1

@NEEONLINE - Okay, it seems like it's an issue with $model->count(). it selects all and therefore ignores find().

2 => array:3 [▼
    "query" => "select count(*) as aggregate from `assets`"
    "bindings" => []
    "time" => 0.52
  ]

Before I'll work on it anymore, I'll upgrade to 5.8... damn.. :/

neeonline's avatar

If you really want just the count (and not get()->count() to make things faster), you can "cheat".

Model::find($id)->select('id')->get()->count();
Model::where('expression')->select('id')->get()->count();

That way you will only retrieve the ID column from the database and perform the count in the results collection.

It's not the most elegant way to do it, but you cannot have to upgrade to 5.8 (which I recommend, but you will have some work).

You can also always do the query yourself

$count = DB::table($tableName)
                     ->select(DB::raw('count(*) as count'))
                     ->where('column', '=', $value)
                     ->first()
                     ->count;

Best,

2 likes
staudenmeir's avatar

Model::find(id)->count() fetches a single instance and then executes a completely new query that counts all rows in the table.

It's equivalent to Model::find(id)->newQuery()->count(), Model::count() or (new Model)->count().

2 likes

Please or to participate in this conversation.