Does your online store feel slow? Slow websites can cost you money. Studies show that when a page takes more than 3 seconds to load, 40% of visitors leave.
In this guide, you’ll learn 15 proven Laravel Performance Optimization Tips for eCommerce that will help your store run faster and smoother.
php
// Add this to your product migration
public function up()
{
Schema::table(‘products’, function (Blueprint $table) {
$table->index(‘sku’);
$table->index([‘category_id’, ‘price’]);
});
}
php
// Bad way – creates many database calls
$orders = Order::all();
foreach ($orders as $order) {
echo $order->user->name;
}
// Good way – one database call
$orders = Order::with(‘user’)->get();
foreach ($orders as $order) {
echo $order->user->name;
}
php
// Cache expensive queries
$products = Cache::remember(‘top_products’, 3600, function () {
return Product::where(‘featured’, true)
->with(‘category’)
->get();
});
php
// Slow way – gets everything
$products = Product::all()->where(‘active’, true);
// Fast way – asks database to filter first
$products = Product::where(‘active’, true)->get();
php
// Get just what you need
$names = Product::select(‘name’, ‘price’)
->where(‘active’, true)
->chunk(100, function($products) {
// handle chunks here
});
php
// Save category menu for 1 hour
$menu = Cache::remember(‘category_menu’, 3600, function() {
return Category::active()
->with(‘subcategories’)
->get();
});
php
// Don’t do this during checkout
Order::create()->process();
// Do this instead
ProcessOrder::dispatch($orderData);
php
// Queue setup in .env
QUEUE_CONNECTION=redis
javascript
// webpack.mix.js
mix.js(‘resources/js/app.js’, ‘public/js’)
.sass(‘resources/sass/app.scss’, ‘public/css’)
.version();
// Make files even smaller
mix.js(‘resources/js/app.js’, ‘public/js’)
.sass(‘resources/sass/app.scss’, ‘public/css’)
.minify()
.version();
php
// in .htaccess
<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType image/jpg “access plus 1 year”
ExpiresByType image/jpeg “access plus 1 year”
ExpiresByType image/gif “access plus 1 year”
ExpiresByType image/png “access plus 1 year”
ExpiresByType text/css “access plus 1 month”
ExpiresByType application/js “access plus 1 month”
</IfModule>
php
// Resize on upload
use Intervention\Image\Facades\Image;
$image = Image::make($request->file(‘product_image’))
->resize(800, null, function ($constraint) {
$constraint->aspectRatio();
})
->save();
html
<!– Lazy load images –>
<img src=”product.jpg“ loading=”lazy“ alt=”Cool product“>
php
// config/session.php
‘driver’ => env(‘SESSION_DRIVER’, ‘redis’),
// .env
SESSION_DRIVER=redis
REDIS_HOST=127.0.0.1
REDIS_PORT=6379
php
// ProductController.php
public function show(Product $product)
{
return cache()->remember(
“product.{$product->id}“,
3600,
fn() => view(‘product.show’, compact(‘product’))
);
}
php
// PaymentService.php
public function getPaymentMethods()
{
return cache()->tags([‘api’])
->remember(‘payment_methods’,
now()->addHours(6),
fn() => $this->api->fetchMethods()
);
}
php
// Clear specific caches
cache()->tags([‘api’])->flush();
php
; php.ini tweaks
opcache.enable=1
opcache.memory_consumption=256
opcache.max_accelerated_files=20000
opcache.revalidate_freq=0
opcache.validate_timestamps=0
nginx
# nginx config
upstream laravel_app {
server 10.0.0.1:80;
server 10.0.0.2:80;
server 10.0.0.3:80;
}
server {
location / {
proxy_pass http://laravel_app;
}
}
php
// config/filesystems.php
‘s3’ => [
‘driver’ => ‘s3’,
‘key’ => env(‘AWS_KEY’),
‘secret’ => env(‘AWS_SECRET’),
‘region’ => env(‘AWS_REGION’),
‘bucket’ => env(‘AWS_BUCKET’),
‘url’ => env(‘AWS_URL’),
]
php
// Use CDN in views
<img src=“{{ asset(‘images/product.jpg’) }}”>
We help businesses make their Laravel stores lightning fast. Our Laravel Consulting Services focus on real results – faster sites, happier customers, more sales.
“These guys really know what they're doing. I've used them for some of my own clients and have always been happy with the results.”
“I have worked with N Technolabs on several projects, I have always received excellent work and communication from N Technolabs, I will continue to hire him for my next projects. I recommend it 100%”
We empower business success through tech and design. Where code meets creativity for digital excellence.
© ntechnolabs 2024. All rights reserved.