Count data from database on laravel

Stefan Izdrail

Founder & Senior Architect · 2026-06-29

Laravel Company
Title: Efficiently Counting Records from Database on Laravel: A Comprehensive Guide Introduction Laravel is an amazing PHP framework for building web applications that make development easier and more efficient. One of its primary features is the simplicity it offers when working with databases. In this blog post, we will explore how to count data from a database using Laravel and present best practices for achieving this task efficiently. Getting Started: Understanding Counting Records in Laravel Counting records in Laravel involves retrieving the total number of results matching specific criteria within a table. This process can be accomplished with ease using the built-in query builder or an Eloquent model. Here, we'll focus on two primary methods to count data from the database: DB::select() and Model::count(). 1. Using DB::select() DB::select() is a convenient method for performing complex queries that are not easily handled by Laravel's query builder syntax. In this case, we can use it to select and count records from our goods table. The code would look like this:
function show(Request $request){
    $c = DB::select("SELECT count(*) as total FROM goods");
    if($c > 0):
        return response()->json(['total' => $c[0]->total], 200); // Return a JSON response with the total count.
    endif;
}
In this example, we are querying the database for the total number of records in the goods table and returning the result as a JSON response with the field name "total." Remember to always check that there is at least one row returned before accessing the data. 2. Using Model::count() When working with Eloquent models, you can use the count() method to get the total number of records for any given table. Here's an example:
function show(Request $request){
    $goods = Goods::all();
    if(count($goods) > 0):
        return response()->json(['total' => $goods->count()], 200); // Return a JSON response with the total count.
    endif;
}
In this case, we are fetching all records from the goods table using Goods::all(), and then calling the Eloquent Collection's count() method to get the total count. The result is again returned as a JSON response with the field name "total." Best Practices for Counting Records in Laravel 1. Use query caching for better performance: Incorporate query caching in your application to improve the speed of counting records and reduce server load. 2. Return count with JSON response: Always return the total count as a JSON response with an appropriate HTTP status code (e.g., 200 for success). This will ensure that consumers can handle responses properly based on their needs. 3. Handle errors gracefully: Validate and sanitize input parameters to avoid SQL injection attacks or other security issues in your application. Always check for empty results before accessing data, and display appropriate error messages when required. Conclusion Counting records from the database is an essential task that developers often need to perform, particularly while working on web applications with large datasets. By following the best practices mentioned above and utilizing Laravel's features efficiently, you can ensure reliable and secure counting of data from your databases.