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

Uncategorized Laravel ajax form with example

Submit form using jQuery in Laravel

November 18, 2021November 18, 2021

jQuery is most popular library to handle the client side events like form submit, click, change etc. In this article we will learn to submit the form using jQuery Ajax method. Ajax is mostly used when we do not want to reload the page and real time interaction. In this…

Read More
Devops How to install PHP

How to install PHP on Ubuntu?

September 18, 2021October 4, 2022

PHP is widely used langauge in world of web. PHP is an open sourece language and free for everyone. It’s a server side scripting lanaguge which compiles on server and can embedded with html too. Currently, there is three version are available of PHP that are as below PHP 5…

Read More
Php How to Send Mail with Attachment(PDF, docs,image) in Laravel

How to Send Mail with Attachment(PDF, docs,image) in Laravel 8 /9 ?

May 10, 2022May 10, 2022

Attachment with mail is very important feature of mail by which we can attach any document with our email , for example in some scenarios we want to send the invoice to user mail or some guideline to user after registration. So in this article we will learn to use…

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

  • July 2025
  • 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

  • Mapping Together: The Vibrant Spirit of OpenStreetMap Japan
  • Understanding High Vulnerabilities: A Deep Dive into the Weekly Summary
  • Building a Million-Dollar Brand: The Journey of Justin Jackson
  • Mastering Schedule Management with Laravel Zap
  • The Resilience of Nature: How Forests Recover After Fires
©2023 Readerstacks | Design and Developed by Readerstacks
Go to mobile version