Backend Development
Laravel
Subjective
Sep 30, 2025
Explain Performance optimization in Laravel with code examples.
Detailed Explanation
Laravel performance optimization involves multiple strategies:
**Database Optimization:**
// Eager Loading
$users = User::with(["posts", "profile"])->get();
// Query Optimization
$users = User::select(["id", "name", "email"])
->where("active", true)
->limit(100)
->get();
// Database Indexing
Schema::table("users", function (Blueprint $table) {
$table->index(["email", "active"]);
});
**Caching Strategies:**
// Query Caching
$users = Cache::remember("active_users", 3600, function () {
return User::where("active", true)->get();
});
// Route Caching
php artisan route:cache
// Config Caching
php artisan config:cache
**Queue Optimization:**
// Background Processing
ProcessPayment::dispatch($order)->onQueue("payments");
// Batch Jobs
Bus::batch([
new ProcessPayment($order1),
new ProcessPayment($order2),
])->dispatch();Discussion (0)
No comments yet. Be the first to share your thoughts!
Share Your Thoughts