1 year ago

Eloquent find() and count()

Posted 1 year ago by Slamm

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.

Please sign in or create an account to participate in this conversation.