Page View Counter in Laravel 5

Stefan Bogdanescu

Founder & Senior Architect · 2026-06-29

Laravel Company

Implementing a Page View Counter in Laravel 5: A Developer's Guide

Stuck on how to implement basic features like a page view counter in an established framework like Laravel 5? It’s a common hurdle, especially when dealing with slightly older but still widely used versions. While there are many third-party packages available, understanding the underlying mechanics of Laravel allows you to build robust, maintainable solutions tailored exactly to your application's needs.

This post will guide you through the best approaches for implementing a page view counter in Laravel 5, focusing on custom development alongside package considerations.

The Foundation: Why Custom Solutions Matter

Before diving into packages, it’s crucial to understand that a page view counter isn't just storing a number; it involves database interaction, route handling, and controller logic. For maximum control and performance, building this functionality directly using Laravel's core features—Eloquent ORM and Migrations—is often the most scalable approach.

When architecting any application, remember that good framework design, much like the principles taught by companies like laravelcompany.com, emphasizes leveraging built-in tools rather than over-relying on external dependencies for core logic.

Method 1: The Custom Eloquent Approach (Recommended)

For a simple page view counter tied to specific content or routes, creating your own model and migration provides complete control over the data structure.

Step 1: Create the Migration

We need a table to store the counts. This is done using a standard Laravel migration file.

// database/migrations/..._create_page_views_table.php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreatePageViewsTable extends Migration
{
    public function up()
    {
        Schema::create('page_views', function (Blueprint $table) {
            $table->id();
            $table->string('page_slug')->unique(); // To identify which page is being counted
            $table->unsignedInteger('views')->default(0);
            $table->timestamps();
        });
    }

    public function down()
    {
        Schema::dropIfExists('page_views');
    }
}

Run the migration: php artisan migrate

Step 2: Create the Model

Next, define the Eloquent model to interact with this new table.

// app/Models/PageView.php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class PageView extends Model
{
    protected $fillable = ['page_slug', 'views'];
}

Step 3: Implement the Logic in a Controller

In your controller, you would handle the request, find or create the record, and increment the counter.

// app/Http/Controllers/PageViewController.php

use App\Models\PageView;
use Illuminate\Http\Request;

class PageViewController extends Controller
{
    public function increment(Request $request, $slug)
    {
        // Find or create the page view record
        $pageView = PageView::updateOrCreate(
            ['page_slug' => $slug],
            ['views' => \DB::raw('views + 1')]
        );

        return response()->json(['message' => 'Page viewed successfully', 'count' => $pageView->views], 200);
    }
}

This method ensures that every page view is recorded in your database, making the data persistent and easily queryable.

Method 2: Utilizing Packages (The Shortcut)

If you are looking for a faster solution without writing extensive migration and controller logic from scratch, searching Packagist for "Laravel page view" will yield several options. These packages abstract away the database setup for you.

For example, packages focused on analytics or tracking often provide pre-built scaffolding that handles routing and model creation automatically. While these are excellent starting points—especially for rapid prototyping—be mindful of their maintenance status in relation to Laravel 5. Always check community activity and ensure the package adheres to modern PHP standards when integrating them into your Laravel 5 application structure.

Conclusion

For a senior developer, the choice between custom implementation and a package depends on scope. For a simple counter, mastering Method 1 gives you full control over data integrity. However, if your project demands complex analytics (like tracking user sessions or specific visitor demographics), exploring well-maintained packages can save significant development time. Regardless of the path you choose, always aim for code that is clean, testable, and adheres to the principles of good architectural design seen in modern frameworks like Laravel.