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 get random rows in laravel example

How to get random rows in laravel example ?

October 10, 2022March 16, 2024

In this blog post, we’ll take a look at how to get random rows in Laravel database. We’ll explore the use of the QueryBuilder, Eloquent, and the DB facade. laravel has inbuilt eloquent method to get the random records from database using the inRandomOrder method. we will understand the random…

Read More
Php Multiple Ways to Apply Unique Validation with Soft Delete in Laravel

Multiple Ways to Apply Unique Validation with Soft Delete in Laravel

June 17, 2022June 17, 2022

In this article we will solve the problem of Unique Validation with Soft Delete in Laravel. Soft delete trait in laravel by default add the delete_at null query to all queries but in validation rules of unique it doesn’t apply because validation rule does not use Laravel eloquent model and…

Read More
Php Static keyword

What is static keyword and properties in a php/java class?

June 13, 2021September 21, 2021

Static keyword are isolated, which means we can access the property of a class without creating a object/instance of class. static methods that are common to all the objects of the class. Hence, any logic which can be shared among multiple instances of a class should be inside the static…

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