Skip to content
Readerstacks logo Readerstacks
  • Home
  • Softwares
  • Angular
  • Php
  • Laravel
  • Flutter
Readerstacks logo
Readerstacks
Laravel count rows with example

How to use Laravel count rows with example ?

Aman Jain, January 19, 2022November 9, 2023

MySQL count method gives the total number of rows in executed query, In Laravel we use eloquent or query builder to execute the query. In Laravel eloquent or query builder we use count method to count the rows. We can also count rows after executing the query using get() or all() and then can apply collection count method.

Syntax of Laravel count rows

Here is the syntax for count

User::where("status",1)->count();  // uses mysql count method and more performant

//or

User::where("status",1)->selectRaw("count(column_name_or_*) as counter")->first();  // uses mysql count method and more performant

//or

$users=User::where("status",1)->get(); //execute the query  and use laravel collection count method
$users->count(); // then count all collection

SQL of above query will be

select count(*) from `table` where  `status` = 1

//OR

select count(column_name_or_*) from `table` where  `status` = 1

//OR

select * from `table` where  `status` = 1 

//then count collection of  rows 

So let’s understand count rows with examples

Example 1 – Laravel count with mysql count method (Recommended)

In this example I will show to use MySQL count method in laravel8 eloquent and its recommended since it’s more performant. In this method MySQL count all rows without return all rows data so if there is 10000 rows in database then it will simply return the count only

<?php
  
namespace App\Http\Controllers;
  
use Illuminate\Http\Request;
use App\User;
  
class ArticleController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $Article = Article::select("*")
                        ->where("status",1)
                        ->count();
      \\or
      DB::table('articles')->where("status",1)->count();

   }
}

Output will be :

select count(*) from `articles` where `status` = 1

Example 2 – Laravel count with laravel collection count method

In this example I will show to use laravel eloquent method in eloquent and it’s not recommended since it return all rows and then perform laravel count method so it can be slow in can of records are high.

<?php
  
namespace App\Http\Controllers;
  
use Illuminate\Http\Request;
use App\User;
  
class ArticleController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $Article = Article::select("*")
                        ->where("status",1)
                        ->count();
        $Article->count();
    }
}

Output will be :

select * from `articles` where `status` = 1 

// and then we use laravel collection count
 $Article->count();
 

Related

Php Laravel countEloquentlaravelmysql

Post navigation

Previous post
Next post

Related Posts

Php How to Send Mail in Laravel Through Sendmail and SMTP

How to Send Mail in Laravel 8 / 9 Through Sendmail and SMTP ?

May 8, 2022August 20, 2022

Email is very common operation of any website like sending an email to users after registration, Send newsletters to users and many more. In this tutorial i will show you to send Mail in Laravel Through Sendmail and SMTP. Laravel provides multiple drivers or services to send mail from different…

Read More
Php Laravel password hash and check

How to create hash password in laravel 8 ?

January 26, 2022January 26, 2022

Laravel provides robust security features and one of them are hash password which is not decryptable. For hashing the password laravel use secure Bcrypt and Argon2 hashing for storing user passwords. Password encrypted with Bcrypt can not be decrypt since it uses key to generate the hashed string and irreversible…

Read More
Php How to access and setup storage files after upload in laravel

How to access and setup storage files after upload in laravel ?

May 23, 2022May 26, 2022

In this article i will show you to access and setup uploaded storage files in laravel. This is strongly recommended that not to use direct URL from storage, laravel internally handle it by creating Symblink but some developers access the files of storage directly without knowing the consequences of website…

Read More

Aman Jain
Aman Jain

With years of hands-on experience in the realm of web and mobile development, they have honed their skills in various technologies, including Laravel, PHP CodeIgniter, mobile app development, web app development, Flutter, React, JavaScript, Angular, Devops and so much more. Their proficiency extends to building robust REST APIs, AWS Code scaling, and optimization, ensuring that your applications run seamlessly on the cloud.

Categories

  • Angular
  • CSS
  • Dart
  • Devops
  • Flutter
  • HTML
  • Javascript
  • jQuery
  • Laravel
  • Laravel 10
  • Laravel 11
  • Laravel 9
  • Mysql
  • Php
  • Softwares
  • Ubuntu
  • Uncategorized

Archives

  • June 2025
  • May 2025
  • April 2025
  • October 2024
  • July 2024
  • February 2024
  • January 2024
  • December 2023
  • November 2023
  • October 2023
  • July 2023
  • March 2023
  • November 2022
  • October 2022
  • September 2022
  • August 2022
  • July 2022
  • June 2022
  • May 2022
  • April 2022
  • March 2022
  • February 2022
  • January 2022
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • August 2021
  • July 2021
  • June 2021

Recent Posts

  • The Resilience of Nature: How Forests Recover After Fires
  • Understanding Laravel Cookie Consent for GDPR Compliance
  • Understanding High Vulnerabilities: A Critical Overview of the Week of May 12, 2025
  • Installing a LAMP Stack on Ubuntu: A Comprehensive Guide
  • Understanding High Vulnerabilities: A Deep Dive into Recent Security Concerns
©2023 Readerstacks | Design and Developed by Readerstacks
Go to mobile version