Skip to content
Advertisement

Laravel 5.6.9 – database count gives different outputs

Does anyone know why these two ways to count numbers of users in my table give different answers when i run them in tinker?

AppModelsUser::count() => 92269

$count = DB::table(‘users’)->count() => 92829

Running a SQL query in Sequel Pro gives 92829.

Advertisement

Answer

If you have the SoftDelete trait on your User model then when you query via the Model it excludes “deleted” entries. You can include them by adding the withTrashed() constraint.

AppModelsUser::withTrashed()->count();

https://laravel.com/docs/7.x/eloquent#soft-deleting

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement