Raviya Technical
Published in

Raviya Technical

Laravel Advance | Laravel Model Caching -Performance Boost Tutorial

In this tutorial, we will discuss how to improve performance in laravel 6 using laravel model caching. we can Performance Boost using eloquent model caching via genealabs/laravel-model-caching git composer package in laravel 6, laravel 7, and laravel 8.

As we know, website performance is very important for the site owner. If your website working smoothly then traffic will be more and if you used eloquent cache then it’s no longer load on the server too. so in this post, I will show you how to make mode caching and make performance boost your laravel application.

there are several ways to improve the performance of the laravel website. As you know we almost looking to cache using htaccess and cache images, CSS, js, and HTML files. if you cache CSS, js, HTML then it improves the performance of the website that I did in my previous tutorial, you can read from here

But if you have to fetch many records from a database and from many tables. so when you open a link then more than one query always runs on your web page. it takes time to fetch data from the database because it same process always when you load a page, so basically it loads every time. For a long time when you have more data and mode visitors coming to our website then your server will be broken, but if you use laravel cache then it can save your server and improve page performance. Here we will use genealabs/laravel-model-caching package for model caching.

Laravel cache is a very easy and simple way to use. Using Cache you will optimize your website page speed. So let’s just follow the below step and see how it works. We will use barryvdh/laravel-debugbar package also, this package will help to debug how many queries fire on your page so you can see that. So just follow the below step.

Step 1: Install Laravel 6 App

we are going to from scratch so, we need to get a fresh Laravel 6 application using bellow command, So open your terminal OR command prompt and run bellow command

composer create-project --prefer-dist laravel/laravel blog

Step 2: Installation Of barryvdh/laravel-debugbar Package

Now we will install barryvdh/laravel-debugbar composer package using by following command in our laravel 6 application. So let’s run the below command.

composer require barryvdh/laravel-debugbar --dev

Ok, after installing the package successfully, we will add the service provider in the app.php configuration file. So let’s add as below


<?phpreturn [....'providers' => [....Barryvdh\Debugbar\ServiceProvider::class,],'aliases' => [....'Debugbar' => Barryvdh\Debugbar\Facade::class,]]

Then you can publish configuration files by following the command

php artisan vendor:publish --provider="Barryvdh\Debugbar\ServiceProvider"

Step 3: Installation Of genealabs/laravel-model-caching Package

Now we will install genealabs/laravel-model-caching composer package using by following command in our laravel 6 application. So let’s run the below command.

composer require genealabs/laravel-model-caching

Step 4: Update User Model

In this step, we will use GeneaLabs package class in the User model So you have to update the user model like as below:


<?phpnamespace App;use Illuminate\Notifications\Notifiable;use Illuminate\Foundation\Auth\User as Authenticatable;use GeneaLabs\LaravelModelCaching\Traits\Cachable;class User extends Authenticatable{use Cachable;use Notifiable;/*** The attributes that are mass assignable.** @var array*/protected $fillable = ['name', 'email', 'password',];/*** The attributes that should be hidden for arrays.** @var array*/protected $hidden = ['password', 'remember_token',];}

Step 5: Create Dummy Users

here we will create some dummy user records using a factory. so you can run the below command to create dummy users in your database.

php artisan tinkerfactory(App\User::class, 100)->create();

Step 6: Create Route

now will create one route for display users in view. so let’s create one route as bellow listed:


Route::get('users', 'UserController@index');

Step 7: Create Controller

we need to create a new controller as UserController and then we will create index() on the controller. we will get all users using the User model and print in view:


<?phpnamespace App\Http\Controllers;use Illuminate\Http\Request;use App\User;class UserController extends Controller{/*** The application's index** @var array*/public function index(){$users = User::where('id', 1)->get();$users = User::where('id', 5)->get();$users = User::get();return view('users',compact('users'));}}

Step 8: Create Blade File

now at last we will create users.blade.php file and write code of display users lists. so let’s create a blade file:


<!DOCTYPE html><html><head><title>User List</title><link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" crossorigin="anonymous"></head><body><div class="container"><h1>User List- ItSolutionStuff.com</h1><table class="table table-bordered"><tr><th>Id</th><th>name</th><th>email</th></tr>@foreach($users as $user)<tr><td>{{ $user->id }}</td><td>{{ $user->name }}</td><td>{{ $user->email }}</td></tr>@endforeach</table></div></body></html>

Now you are ready to run the project, so let’s check.

You can cache clear and check again:

php artisan cache:clear




PHP, Laravel And Flutter Development

Recommended from Medium

Deploy Ghost blog using Docker and Caddy

Solve business problems and reduce your IT spend with better computing power

Left View of a Binary Tree

*AWS Cloud Migration:

11/12/2020 7:51PM

| Engineering News-Record

Visualising Google Cloud

PHP In-Depth Introduction To Arrays

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Raviya Technical

Raviya Technical


More from Medium

Laravel Advance | How to Store Multiple Select Values in Laravel?

Log activities in Laravel

Laravel 9 Auth Login and Registration

Features of Laravel 9

Features of Laravel 9