With over a million websites powered by Laravel, Google pushing the importance of website speed, and users less & less accepting of anything other than an incredibly smooth user experience – some are giving PHP & frameworks like Laravel the reputation of being less performant than other frameworks. While there very well may be truth to this, that doesn’t mean there isn’t anything you can do about it – so, in this guide, we’ll dive deep into how you can optimize Laravel for performance. 

Expert Tips on Optimizing Laravel

This article will go over several important tips, each with step-by-step guides to optimize your Laravel website. While some of the steps may sound technical, the steps will be easy to follow and recreate on your own screen.

Using Artisan Commands

Although Artisan commands are commonly listed as part of the optimization tips, it’s not really a hack. Artisan is just a Laravel command line interface that helps you execute instructions through simple commands. We’ll be using a lot of these commands later on.

Artisan Optimize Command

A quick search online will reveal many articles recommending the use of the Artisan Optimize Command. While this used to help, with much improved PHP op-code caching it’s no longer relevant. It was deprecated in Laravel 5.5, and since 5.6 it’s been removed completely. So don’t go looking for this solution as it will no longer exist!

Config Caching

Any web developer today will certainly agree that caching your files is a great way to save on loading time and increase website performance. By generating a ready, easy-to-read template of your website that your servers read instead of all your web files every time someone loads your site, loading times are cut dramatically.

The only problem with caching is that it sets your website to a semi-permanent state. If you plan on making changes anytime soon, you’ll have to undo the cache before configuring your website and then redo it when you’re done. 

Aside from that, caching with Laravel is considerably easier than other frameworks because you can do so through caching engines that don’t involve coding. 

If you’re ready to cache your website, simply type this in the Artisan command line: 

php artisan config:cache

Remember, any changes you make on your site after this won’t have any effect because of your cache. If you want to refresh the cache config, simply type the command again. Otherwise, you can also clear the cache by typing in:

php artisan config:clear

There are multiple ways you can cache with Laravel, but these two basic commands are enough to get you started.

Route Caching

A route cache is a collection of routes around your website that helps load your pages faster. This works by avoiding the taxing and sometimes slow process of mapping your routes from scratch. With route caching, Laravel can temporarily load the routes from a pre-compiled cache instead of starting from scratch for every new user. The cache lasts only until the user exits out of your website.

Just like config caching, any changes you make to your website after the cache will not have any effect until you update the route cache. It’s important to remember to update the cache after making any changes to your website’s structure.

Here’s the command that you have to run to start the route cache:

php artisan route:cache

To clear the cache, you use a similar command:

php artisan route:clear

Route caching is a simple way to make your website feel snappier and load faster.

Use Queues

Laravel queues work like your CPU. Whenever your computer processes a task, it does so in the most efficient way possible that doesn’t degrade the quality of the user’s experience. This means that when you’re rendering a file or doing something resource intensive, your CPU makes sure that you still have processing power left for your other tasks until its limit is reached.

The way this works is that your CPU queues tasks after each other, with no strict adherence to order. If you’re rendering a file, then opening a browser window, your CPU will insert the browser process earlier to make sure it opens without delay. Just like a CPU, Laravel can also improve web performance by offloading time-consuming processes to a queue. 

Simple examples of this are:

  • Sending emails
  • Downloading files
  • Uploading files

These tasks don’t need to be seen by the user and can be done as a background process.

Queues are an in-depth trick though and we, unfortunately, don’t have the space to cover it fully within this article. Instead, you can check out the official Laravel documentation for this. If you’re curious about what the basic commands look like, here are some examples from Artisan:

php artisan queue:work
SendEmailJob::dispatch($to, $body);

Laravel also has several queue driver support documentations and offers unique solutions for each, such as Horizon, a dashboard that monitors your queue system.

Use Eager Loading

Eager loading is a way of coding rather than a simple command that you can run via Artisan. 

Eager loading is the process of sending a larger, nested data structure with more than enough information for the query. Although sending a larger query might require more memory or data, since the internet is already at blazing fast speeds, several more kilobytes or even megabytes will be barely noticeable for most users. These larger data queries are better for the server since it means sending one large query, rather than processing several hundred queries just to look for the user’s data. 

The opposite of this is lazy loading, which might be what your website is utilizing by default, where single items of data are retrieved individually as and when required, resulting in many more server requests. Here’s what the query for that looks like:

$books = App\Book::all();
Foreach ($books as $book) {
Echo $book->author->name;}

Eager loading on the other hand looks like this:

$books = App\Book::with(‘author’)->get();
Foreach ($books as $book) {
Echo $book->author->name;}

A simple example where this is used is in games. Most games use eager loading when preparing levels or discrete phases. Whenever you’re loading into a map, the game loads everything in advance so that you have all the assets you need — even if you don’t explore or use all the objects inside that map. 

The main downside to eager loading has always been the amount of information users would need to download or prepare to access your site. Each query would mean sending a file that’s larger than technically necessary. However, with mobile data and home Wi-Fi fast becoming near-gigabit speeds, file sizes have gone down the priority list when it comes to optimization.

Clear Up Unused Service

Another tip that involves how your code is clearing up any services that are unused by the user.

It’s common to add and start a service when your website loads, but unused services can add unnecessary burdens on your system and strain your server speed. This is especially apparent when handling larger amounts of visitors. You may even have services that were utilized at the beginning of your development process but are no longer used today. 

You can open the Laravel app and go to:


This will show a list of all the services installed on your server. Simply comment out the services that are unused.  

NOTE: be careful with the services you’re trying to disable. You may accidentally disable a service that plays a huge role in your site, causing certain parts of your website to malfunction or not start at all.

Run tests and check the services manually on staging machines first before disabling services you aren’t sure about.

Remove Dev Dependencies

Dev dependencies are often injected into your system by default when installing Laravel or a composer for the first time. While these dependencies do aid in building your website, you don’t need these dependencies when your site is up and running. 

You can input this simple command through Artisan to remove those dependencies:

composer install --prefer-dist --no-dev -o

After running this command a package or directory will be created (possibly a zip file or similar) which you can then use to install the release. The command specifies only to retrieve and package the official distribution, without dependencies.

NOTE: Dev dependencies are different from dependencies that are required at runtime. Don’t delete runtime dependencies as this may affect your website’s performance or even crash certain parts of your site. 

General Web Optimization Tips

There are also several optimization hacks that you can do for your website outside of Laravel. If you’re looking to polish every aspect of your site and increase its performance and rankings on Google, then here’s what you should do next.

Use a Fast Host

A fast host is undeniably the first thing you should start looking at. Is your host fast enough and can it handle the number of visitors that your website is currently receiving, or which you anticipate will be visiting following your business’s growth?  This is an essential question to ask at the moment with Google actively punishing websites which it deems too slow (including speed responses measured in milliseconds).

Going for a dedicated 3rd-party host is always the best option. Shared hosts are too slow and hosting physical servers yourself is expensive and hard to scale. 3rd-party hosts on the other hand will cost you between $25 to $150 per month on average and will provide you with blazing fast speeds, secure connections, and website optimization tools.

Minify CSS and JS Files

Your CSS and JS files are laid out to be easily read by default. This means your files have spaces, indentations, and other formatting that make it easy to read for us. However, your website doesn’t care about these visual syntax practices and can easily read your files without them.

Minifying your CSS and JS files means removing all of the unnecessary spaces, indentations, and other characters that take up valuable server space. Depending on the size of your website, this can make a significant difference in server response times. Just remember to keep a separate copy of the CSS and JS files that aren’t minified, in case you ever need to edit or review your code.

This process can be carried out very quickly by using any one of the many online CSS and JS minifiers available for free. 

Compress Your Images

Images are one of the biggest performance leaks in websites that aren’t optimized. This is especially true for those who upload 1920×1080 images only display it in a 540×300 format. It may be that you upload an image at the largest size you think you’ll ever need, and then just have it rendered down to the actual size in the browser, but this is hugely damaging to response times, and having multiple image files set at all of the needed resolutions is far better.

Compressing your images will not only reduce the sizes of your image files by more than half, it will also retain the quality pretty well. Typically, you’ll have to compress the images before uploading them to your website, but there are several plug-ins and online services that will compress all the images on your site for you. From our experience, a lot of these will need to be paid for.

As either an alternative or additional solution we often recommend clients use a CDN (Content Delivery Network) which helps boost server response times too.

Reduce the Number of HTTP Requests

Another major performance issue is when websites make multiple HTTP requests from the server whenever a visitor loads a web page. Typically this can involve many separate calls from JavaScript files, CSS files, and images.

HTTP requests are basically your computer asking your website for information. Each block of information, whether that’s an image, a link, or any kind of content, will require its own separate HTTP request. Larger files will mean longer request times and many files will mean more HTTP requests. 

To find out the number of HTTP requests your website needs, you can use Google Chrome’s Network panel. You should ideally aim for a number between 10 to 30 files per page. For reference, the average number of requests is typically around 60 to 75 requests. 

With Laravel, you can reduce the number of individual HTTP requests easily by using the CodeSleeve/asset-pipeline. What this will do is to combine all of your separate assets (such as JavaScript files and CSS scripts) and combine them all into one single file. This reduces the HTTP requests down to one. Not only that, but it will also minify the code (something we mentioned earlier), which will help speed up the page loading time dramatically.

Use a CDN (Content Delivery Network)

If your website is hosted in the US, then it may be that visitors from across most of the US will benefit from good response times, but potentially there can be significant delays for those accessing your site from other areas around the world.

One solution to this is to use a Content Delivery Service (CDN). These provide a cached version of either your whole site, part of your site, or assets such as images/videos/audio files on various alternate servers around the world.

When a visitor’s browser requests to load a page from your site, the CDN determines their geographic location, and serves them the page, or assets, from the server that is located closest to them, or which provides the fastest response times.

Getting a CDN used to mean subscribing to another 3rd-party CDN service. However, most 3rd-party website hosts already offer CDNs as part of their basic packages. To reduce the number of payments and have your website set up more conveniently, we recommend looking for a host with CDN features instead of finding a 3rd-party CDN service.

Conclusion – Optimize Laravel for Performance

Laravel has made its name around the web-development scene because of how easy it is to use. It’s straightforward and is fast by default. However, Laravel isn’t immune to bloat and can benefit a great deal from general maintenance and optimization tips. 

By using any of the top optimization tips we’ve included here you can combine the fantastic advantages of Laravel with the ever-growing need to keep your website fast and responsive, keeping Google (and your website visitors & users!) very happy. Do you have any specific questions about Laravel? Or think we’ve missed a top optimization tip? Join the conversation by leaving a comment below. 💬