119

I'm having a lot of trouble figuring out how to use this collection to count rows.

$wordlist = \DB::table('wordlist')->where('id', '<=', $correctedComparisons)
                ->get();

I have tried adding->count() but didn't work. I have tried doing count($wordlist). I'm not really sure what to do without needing a second request as a->count() method.

5 Answers 5

205

Answer has been updated

count is a Collection method. The query builder returns an array. So in order to get the count, you would just count it like you normally would with an array:

$wordCount = count($wordlist);

If you have a wordlist model, then you can use Eloquent to get a Collection and then use the Collection's count method. Example:

$wordlist = Wordlist::where('id', '<=', $correctedComparisons)->get();
$wordCount = $wordlist->count();

There is/was a discussion on having the query builder return a collection here: https://github.com/laravel/framework/issues/10478

However as of now, the query builder always returns an array.

Edit: As linked above, the query builder now returns a collection (not an array). As a result, what JP Foster was trying to do initially will work:

$wordlist = \DB::table('wordlist')->where('id', '<=', $correctedComparisons)
            ->get();
$wordCount = $wordlist->count();

However, as indicated by Leon in the comments, if all you want is the count, then querying for it directly is much faster than fetching an entire collection and then getting the count. In other words, you can do this:

// Query builder
$wordCount = \DB::table('wordlist')->where('id', '<=', $correctedComparisons)
            ->count();

// Eloquent
$wordCount = Wordlist::where('id', '<=', $correctedComparisons)->count();
3
  • 19
    It is quicker to access count() directly with a where clause than returning the whole collection. If this was a count of your entire user table for example, it could be return 1m results just to count them, and that would use a lot of resources.
    – Leon
    Jan 6, 2019 at 10:36
  • For me query builder gives count 1 even if there are more results. Using same queryBuilder I return other results.
    – Darius.V
    Sep 6, 2021 at 13:49
  • it works as expected with db:table Sep 23, 2021 at 18:42
138

Direct get a count of row

Using Eloquent

 //Useing Eloquent
 $count = Model::count();    

 //example            
 $count1 = Wordlist::count();

Using query builder

 //Using query builder
 $count = \DB::table('table_name')->count();

 //example
 $count2 = \DB::table('wordlist')->where('id', '<=', $correctedComparisons)->count();
2
42

Its better to access the count with the laravels count method

$count = Model::where('status','=','1')->count();

or

$count = Model::count();
0
7

also, you can fetch all data and count in the blade file. for example:

your code in the controller

$posts = Post::all();
return view('post', compact('posts'));

your code in the blade file.

{{ $posts->count() }}

finally, you can see the total of your posts.

1
  • 1
    Definitely don't do this whenever you only need the number of records in a table. Post::all() will load the entire post table in a collection and then count the number of records in the instantiated collection, resulting in very poor performance when dealing with large datasets. Mar 30 at 12:46
2

//controller $count = Post::count(); return view('post', compact('count'));

//blade {{$count}}

or //controller $posts = Post::all(); return view('post', compact('posts'));

//blade{{count($posts)}}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.