Kritim Yantra
Mar 26, 2025
A slow Laravel application can frustrate users and hurt SEO rankings. In this guide, we'll explore proven optimization techniques to make Laravel 12 super fast—covering caching, database tuning, asset optimization, and advanced server configurations.
Caching reduces database queries and speeds up response times.
php artisan route:cache
php artisan route:clear
when modifying routes.php artisan config:cache
php artisan config:clear
after updates.php artisan view:cache
php artisan view:clear
.Replace file
/database
cache driver with Redis:
CACHE_DRIVER=redis
SESSION_DRIVER=redis
Install Redis:
sudo apt install redis-server
composer require predis/predis
Slow queries are a common bottleneck.
Schema::table('users', function (Blueprint $table) {
$table->index('email'); // Speeds up WHERE email='x' queries
});
❌ Bad (N+1 queries):
$posts = Post::all();
foreach ($posts as $post) {
echo $post->author->name; // Queries DB for each post
}
✅ Good (1 query):
$posts = Post::with('author')->get();
$users = User::paginate(20); // Loads 20 records per page
composer dump-autoload -o
To remove packages that are no longer required, execute the following command. Replace unused/package
with the actual name of the package you wish to remove.
composer remove your/package-name
npm install
npm run build
Update .env
:
ASSET_URL=https://cdn.yourdomain.com
<img src="placeholder.jpg" data-src="real-image.jpg" loading="lazy">
Enable in php.ini
:
opcache.enable=1
opcache.memory_consumption=256
opcache.max_accelerated_files=20000
Nginx Example:
gzip on;
gzip_types text/css application/javascript;
Example Nginx config:
location ~ \.php$ {
fastcgi_pass unix:/var/run/php/php8.2-fpm.sock;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
include fastcgi_params;
}
Offload slow operations (emails, exports) to queues:
php artisan make:job ProcessPodcast
Dispatch jobs:
ProcessPodcast::dispatch($podcast)->onQueue('default');
Run the queue worker:
php artisan queue:work --queue=high,default
composer require laravel/telescope
php artisan telescope:install
php artisan migrate
composer require laravel/horizon
php artisan horizon:install
Enable in php.ini
:
opcache.jit_buffer_size=256M
opcache.jit=1235
opcache.preload=/path/to/project/vendor/composer/autoload_classmap.php
DB_HOST_READ=replica-db.example.com
DB_HOST_WRITE=primary-db.example.com
Optimization | Before (ms) | After (ms) |
---|---|---|
Default Laravel | 350 | - |
By implementing these optimizations:
✅ Reduce response time by 60-70%
✅ Cut database load by 50%
✅ Improve scalability for high traffic
php artisan optimize
after deployment.Your Laravel 12 app is now blazing fast! 🚀
Need help? Let me know in the comments!
No comments yet. Be the first to comment!
Please log in to post a comment:
Continue with Google