How to show Count Data in Laravel 5

Stefan Bogdanescu

Founder & Senior Architect · 2026-06-29

Laravel Company

Stop Confusing Yourself: How to Display Count Data in Laravel

Welcome to the world of Laravel! It’s completely normal to feel overwhelmed when you start dealing with data retrieval, especially when mixing database counts with front-end display. You want to show "Total data: 12" next to your table, but you don't know whether to put that logic in the Controller, the Model, or the View.

As a senior developer, I can tell you that the key to solving this lies in understanding Separation of Concerns. The Controller handles the logic (talking to the database), and the View handles only the presentation (what the user sees).

This guide will walk you through the most efficient, Laravel-idiomatic way to fetch and display row counts, ensuring your code is clean, scalable, and follows best practices. We will focus on using Eloquent to make this process simple.


The Developer's Approach: Where Should the Count Live?

When dealing with data presentation in a framework like Laravel, the logic for calculating something should reside in the Controller or the Model, not directly in the Blade view file. This keeps your application maintainable.

For displaying simple counts related to a specific model (like counting rows in tb_surat), the most efficient place is within your Eloquent Model or directly within the Controller before passing data to the view.

Step-by-Step Guide: Counting Data in Laravel

Let's assume you have an Eloquent Model named Surat that corresponds to your tb_surat table.

Step 1: Define the Route and Controller Logic

You will use your controller method to query the database, get the total count, and retrieve the actual data.

Example Controller Implementation:

In your controller (e.g., DashboardController.php), you would perform two actions: first, calculate the count; second, fetch the records.

<?php

namespace App\Http\Controllers;

use App\Model\Surat; // Assuming your model is named Surat
use Illuminate\Http\Request;

class DashboardController extends Controller
{
    public function showDashboard()
    {
        // 1. Calculate the total count efficiently using Eloquent's count() method
        $totalCount = Surat::count();

        // 2. Retrieve all the actual data for the table
        $suratData = Surat::all();

        // 3. Pass both the count and the data to the view
        return view('dashboard', [
            'total_data' => $totalCount,
            'surat_list' => $suratData
        ]);
    }
}

Step 2: Displaying the Data in the View (Blade File)

Now that you have the count and the data ready in your controller, displaying it in the Blade view becomes straightforward. You simply access the variables passed to the view using double curly braces {{ ... }}.

Example Blade Implementation (dashboard.blade.php):

<h1>Dashboard Overview</h1>

{{-- Displaying the total count --}}
<p class="total-count">Total data: {{ $total_data }}</p>

<h2>Surat List</h2>
<table>
    <thead>
        <tr>
            <th>ID</th>
            <th>Title</th>
            <th>Details</th>
        </tr>
    </thead>
    <tbody>
        {{-- Loop through the retrieved data --}}
        @foreach ($surat_list as $surat)
            <tr>
                <td>{{ $surat->id }}</td>
                <td>{{ $surat->title }}</td>
                <td>{{ Str::limit($surat->details, 50) }}</td>
            </tr>
        @endforeach
    </tbody>
</table>

Why This Approach is Best

This method is superior because:

  1. Efficiency: By using Surat::count(), you instruct the database to perform a single, highly optimized query to get the total number of rows, which is much faster than fetching all records and then counting them in PHP.
  2. Readability: The Controller remains focused on data retrieval, and the View remains focused purely on presentation. This adheres perfectly to the principles taught by Laravel and promotes clean code structure. If you are learning more about building robust applications with Laravel, understanding these foundational patterns is crucial, much like when exploring the power behind frameworks like Laravel.

Conclusion

Showing aggregate data like a row count is a common task in web development. Forget getting lost trying to find the right file—the solution is always about where the data is calculated and how it is passed. By leveraging Eloquent methods like count() in your controller and simply echoing those variables in your Blade view, you establish a solid, scalable pattern. Keep practicing these concepts, and you will master building powerful applications with Laravel!