How to optimize your Laravel application performance

Laravel has proved best PHP framework for small and large size projects. Unlike CMS system, Laravel has huge scope for customization and lacks out-of the box plugins for optimization. But if you follow few steps for optimization, then your Laravel application  page speed and performance will surely improve.

In this article, we will discuss on few tips one by one on improving your Laravel website application performance.

Cache config, views, routes

In Laravel all your configuration files stored in config folder. We can cache all these configuration into single file with below command.

php artisan config:cache

When you want to refresh the cached config, first you need to clear the configuration with below command. 

php artisan config:clear

Same way when you cache views, it will compile blade templates and store them into storage folder. You can cache views with below command.

php artisan view:cache

And you can also cache all your website routes into single route file

php artisan route:cache

Optimize images

Images consume more memory and space than anything. So while loading the pages it slows the webpage loading. To optimize images, you can use Cloudinary or TinyPNG services.

Cache Queries Results

In your Laravel application, for some modules you can cache query which are not updated frequantly. Like in articles view, if you are updating articles one time for day, you can cache articles for few hours. The below query will cache articles for 1 hour.

$articles = Cache::remember('articles', 3600, function () {
    return DB::table('articles')->get();

The above will call query to database after 1 hour, until it articles will be loaded from cache.

Composer Autoload optimization

Composer autoload is single command optimization feature. This will generate class map and ultimately it  makes Laravel little faster. The below command will optimize composer autoload.

composer dump-autoload -o
php artisan optimize

The above command will clear the current compiled files, update the classes it needs and then write them so you don't have to do it again.

Eager Loading

Laravel provides eager loading while accessing relationships data. In Lazy loading method, relationship data is not loaded until you access the property. But in eager loading, the relationship data loaded when you query parent model. This reduces operation time for two queries.

When running query, you can specify which relationship data will be eager loading. Suppose you want to get author details with articles data. You can specify this in Article model.


namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Article extends Model
     * Get the author details with articles
    public function author()
        return $this->belongsTo(Author::class);

Now you can get author data with articles. This will call single query for articles' author details instead of calling one by one query in loop.

$articles = Article::with('author')->get();

foreach ($articles as $articles) {
    echo $article->author->name;

Use CDN for static asset files

Loading static files like Javascript, CSS and images from CDN server will increase speed for faster delivery. It will provide assets from nearby client server. There are many CDN assets providers like cloudflare, MaxCDN, Google etc.

Compile CSS and Javascript files

Laravel Mix comes preinstalled in Laravel application. It compiles all your css files into single app.css file and javascript to app.js file.

let mix = require('laravel-mix');

mix.js('resources/js/app.js', 'public/js')
    .postCss('resources/css/app.css', 'public/css');

You just need to install mix with NPM command.

npm install

And run the below command to build mix in single file. When you run the dev or production scripts, all of your application's CSS and JavaScript assets will be compiled and placed in your application's public directory:

npm run prod

You can find more information here.

Use Laravel queues

For some backend tasks like sending email and notifications, always use Laravel queues. This will send mails when your server is less working.

Choosing fast Web hosting server

This factor mostly affect your website speed. A good web hosting server serves webpages faster. Choosing dedicated or VPS over shared server is good option if you have afford it.


There is always scope to increase website speed and optimization. The above are just few examples to increase Laravel application speed and performance. Different developer has his/her own techniques to optimize website and make faster.