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 Install Composer

What is composer and how to install composer?

September 18, 2021September 21, 2021

Composer is a dependency manager tool for PHP. Using the composer we can manage the package dependency and version of the packages. Composer is different from npm (Node package manager) and apt-get because it only contain the dependency of project. Composer creates a compose.json file to contain the information about…

Read More
Php Laravel 9 Ajax CRUD with Search, Image and Pagination

Laravel 9 Ajax CRUD with Search, Image and Pagination

June 26, 2022March 28, 2023

In this article i will learn Ajax CRUD with Search, Image and Pagination in laravel 9. When we want to perform the CRUD operations without reloading the page then we use javascript and Ajax to refresh the page content without reloading.In this post we will not only cover the Ajax…

Read More
Php How to Share Page to Facebook in Laravel PHP

How to Share Page to Facebook in Laravel PHP ?

March 16, 2022March 16, 2022

Sharing page to facebook from a website using a link is simple as possible. In this article we will not use any package or third party tool to share the url or message to Facebook. Facebook itself provide to share URL using the SDK or a simple url share ,…

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