Tips & Tricks

Top Laravel Performance Optimization Tips

Pinterest LinkedIn Tumblr

Top Laravel Performance Optimization Tips

If you run a business and want to build a web app with the Laravel PHP framework, then you’ve come across the right article! Most business owners like to use the Laravel framework to make web apps. Laravel has a strong and graceful syntax that makes it easy to write code.

Laravel makes web development easier and simplifies some tasks that are done over and over again, like cashing, authentication, and routing. It can be used for everything from small websites to web apps for big businesses. It helps make the task as easy as possible.

Today, Laravel has become a very popular framework for making business and e-commerce apps, and it is used a lot. Most businesses use Laravel to build their business apps. There are a lot of reasons for that. Also, there are many ultimate steps to improve Laravel application performance; let’s discuss them in detail.

Why Do We Need To Improve Performance?

As we talk about Performance and Optimization, these are the two main things that make a Laravel application work. Performance is the most important part of the app that laravel developers should pay attention to before they deliver their laravel app while optimizing the code directly controls the efficiency of the application.

I have some of the most important tips that you can use to improve the code of your application and make it run faster. Let’s discuss!

Best Laravel Performance Optimization Tips

1. Eager Loading

When we use Laravel to get information from a database, it loads the records when we need it. Which is a good thing to do. We don’t have to do this when we’re loading relational data, though. This slows down the performance, and this process is called Lazy Loading.

In some cases, this “lazy loading” behavior increases the number of queries that are run and at the same time slows down the performance of the app.

Below is a simple example that will help us understand how lazy loading works and how we can fix it, too.

All of our books have an author.

With lazy loading, we will have to run N+1 queries to get the answer.

In this case, $books = App\Models\Book::all();

In this case, $books = App\Models\Book::all(); foreach ($books as $book) {

echo $book->author->name;


In the above code, every time the ‘foreach’ loop runs, the query to get the author name is run. Here are the queries that are running:

Select * from books;

Select * from authors where Limit 1;

Select * from authors where Limit 1;

Select * from authors where Limit 1;


To solve this problem, Laravel has a function called ‘with()’ that allows us to load all of the relational data at once and put it in the right place.

When you do this, the number of queries that will be run will go down, and the execution time will go up at the same time. The code sample below shows how we can quickly load all of the data before we start processing it.

In this case,

$books = App\Models\Book::with(‘author’)->get();

foreach ($books as $book) {

echo $book->author->name;


After implementation of eager loading, queries running behind:-

Select * from books;

Select * from authors where in (?, ?, ?, ?);

So, it’s a good idea to use eager loading whenever you need to get relational data.

2. Eloquent Paginate, Chunking, Cursor

Laravel Eloquent has a lot of powerful tools for us, like Paginate, Chunking, Cursor, and more. Most of the time, we use Laravel’s get() or all() functions to get or all of the data we need. This works well for small amounts of data. Even though there aren’t many laravel application performance issues when we talk about a large dataset when there are a lot of models that need to be processed, you can use the paginate() or chunk() function to speed things up.

To show how Paginate can be used, here is an example:

use App\Models\User;


Passing Eloquent models to a processing closure, the chunk method will only get a small group of models. When working with a lot of models, the chunk method will save a lot of memory because only the current chunk of Eloquent models are changed at a time.

use App\Models\User;

User::chunk(100, function ($users) {

foreach ($users as $user) {




As soon as you start iterating over an Eloquent model, it gets hydrated. The cursor method only runs one database query. It’s because while going over the cursor, only one model of Eloquent will be kept in memory at a time, so only one model of Eloquent will be kept in memory at a time. The cursor method helps the application use less memory when iterating a lot of records using the Eloquent Model.

use App\Models\User;

foreach (User::cursor() as $user) {



3. PHP Artisan Route Cache

Laravel lets us store application routes in a cache. This is very important when an application has a lot of different routes. First, Laravel reads all the routes from the route files. Then, it turns them into an array. People who use laravel often refresh the screen or make requests. Every time laravel interprets routes from the PHP file turns them into an array and uses those.

Laravel, on the other hand, gives us a single instruction to read all the routes of the application, translate them to an array, and store them in a cache.

Example  – php artisan route:cache

Every time this code is run, make sure to run it again when the route files are changed. Otherwise, laravel will load old routes from its cache and new changes in the route file will not show up. To get rid of the route cache, use the command below:

php artisan route:clear

The development server should not be used to run the route-cache command, so keep that in mind. This command was made for use in production environments.

4. PHP Artisan Config Cache

This is the same thing that Laravel can do for us. Laravel allows us to store all of the routes and all of the application configurations.

PHP artisan config: cache

This will make it easier for the application to load all the settings into one file.

5. Reduce Package Usage

Many people use open-source software, and Laravel is very well known. Almost every day, there are more and more packages being released or there are new releases in existing packages. Then, you can use these packages and their features right away in your app.

For these packages, you must put them in the composer.json file. Laravel will then install these packages and their dependencies.

There are a lot of things we need to think about before adding new packages into an application. They are not all made to do the same thing. There are a lot of different things that some of the packages can do. This is how it works: If you add a lot of packages that have a lot of dependencies, then the size of the application will also go up, so it will be a lot bigger.

6. Queues

Queues are how you handle requests and heavy tasks. They are the process. It has a direct effect on the user’s experience.

As an example, when a user signs up for the website, you have to do a lot of things in the backend. For example, you have to store the user’s information, send an activation email, and send a welcome email. If you send a mail without having to wait in line, it will take about 4-5 seconds. Users have to wait for the request to come in before they can do anything else So, with queues, you just need to push actions into the Queues after you do the necessary checks and show the user a success message. After that, you just need to deal with simple things when queues run.

7. Remove Unused Services

The service container in Laravel makes it easy to add services to the application. When you write the config/app.php file, you can add any service we want to use to the providers’ list.

Even though you often use services that do not need to be used in every part of your applications. So, if you add these services to the load, that will have an effect on the performance of the application.

As a way to get around this, you can put the service where it is needed. It is possible to put a service into a class like a controller. There are two ways.

you’ll sometimes need to add a service to a class before it is loaded. So that when you load the service, you can use it anywhere in the class afterwards.

In some cases, you’ll only need service in one method of a class. You can put the service inside that method. Service would then have to add inside the function by itself, so it would have to do that.

By using method injection, you can lessen the load on the application when it is loading and make it run faster. So you need to make sure that there aren’t any services that aren’t used in the config/app.php file that we don’t need.

8. Assets Bundling

A package called Laravel Mix comes with all Laravel applications by default. This package is called Laravel Mix. Laravel Mix has an easy-to-use API that lets you set up Webpack build ups for your PHP applications with a variety of common CSS and JavaScript preprocessors. Laravel Mix is a great tool for compiling application assets like JavaScript, CSS, and more. With the help of Laravel Mix, you can put together a lot of different CSS files into one file.

For example,

mix.js(‘resources/js/app.js’, ‘public/js’)

.sass(‘resources/sass/app.scss’, ‘public/css’);

9. Assets Minifying

As you know, you can put a lot of css and js files together to make it bigger. Even if they did that, it wouldn’t help with their performance. So, to solve this problem, you can use Laravel Mix to make these files smaller.

In the command below, you can shrink the files down.

npm run prod

Usually, developers run this command when their assets are ready to be used in a project. Their files and assets will be smaller after they run the command. This will make them easier to find and speed up the performance of the app. Besides this, you can also use a Content Delivery Network (CDN), which is a group of servers that are spread across the world and work together to make the Internet content faster to get to people.

A Content Delivery Network (CDN) makes it easy to quickly move assets needed to load Internet content, such as HTML pages, JavaScript files, stylesheets, images, and videos, from one place to another. A CDN service has been a popular choice for people ever since it was first made. The majority of web traffic is now served by CDNs, like traffic from brands like Amazon, Facebook, and Netflix.

10. Composer Optimize Autoloader

As soon as a request comes in, Laravel will automatically load all of the classes it needs. When Laravel needs to load all the classes, it needs to go through the whole file system to find the ones that it needs.

This is very useful when you are developing. you don’t have to change the autoload configuration every time you add a new file. The problem is that if a file has to be looked for in the file system every time, that will slow down the app.

Because Composer is so slow, you can use class mapping. The composer scans all the classes on your computer and makes a map of them. You can just read a single file now, which holds all the class mappings. That makes the application run faster.

11. Fast Cache or Session Drivers

To speed up Laravel apps, you can store sessions and cache in RAM. As a cache, Memcached is the best one. It is also the fastest. Laravel is very easy to move a cache or session drive to another.

For the session driver, you can change the driver key in the config/session.php file, and for the cache, you can change the driver key in the config/cache.php.

12. Database Indexing

People who work with Laravel do a lot of things to improve the speed of their applications. These include things like caching and data loading. Then, there is one more thing you can do to make things run faster, which is to index the database. This is mostly a database level method.

Indexing is a way to group data in a database table based on a single or a group of columns of the table. Indexing is a way to make it easier for people to find the information that they need quickly. It makes it easier to find the data quickly, without having to go through each and every row every time the database is used.

Using columns, indexing helps to reduce the number of times the disc has to be accessed for each query that is run. When you make database indexing a powerful tool for optimising the database, it also improves how well the whole database works.

This is how you make indexing in Laravel. You can do this with migrations.

Below, you can see an example of how this works:

Schema::create(‘users’, function (Blueprint $table) {




In the end, there are many more ways to make the application run faster but as a starter, you can consider all the tips discussed above. This way, the developer who works with Laravel can make sure that the software they are making runs quickly and is optimized for performance.

Comments are closed.