📡 You're offline — showing cached content
New version available!
Quick Access
Tutorials Laravel Framework Performance Optimization

Performance Optimization

5 min read Quiz at the end
Use php artisan optimize, eager loading, select(), chunk(), and DB indexes to speed up production.

Laravel Performance

# Optimize for production
php artisan optimize              # cache config+routes+views
php artisan config:cache
php artisan route:cache
php artisan view:cache
php artisan event:cache

# Eager loading — always use to prevent N+1
$posts = Post::with("user", "tags")->get();

# Use select() to limit columns
User::select("id", "name", "email")->get();

# withCount instead of count in loop
User::withCount("posts")->get();

# Use chunk() for large datasets
Post::chunk(500, fn($posts) => $posts->searchable());

# Queues for heavy tasks
dispatch(new GenerateReport($data));

# Enable OPcache in PHP
# opcache.enable=1 in php.ini

# Index database columns
$table->index(["user_id", "created_at"]);
Topic Quiz · 3 questions

Test your understanding before moving on

1. What does php artisan optimize do?
💡 php artisan optimize pre-caches everything for production performance.
2. Which method prevents N+1 queries?
💡 with() eager loads relationships in fewer queries.
3. What is the N+1 query problem?
💡 N+1 means 1 query for main records then N queries for each relationship.