5 Laravel Tricks That'll Make Your App Fly 🚀
5 Laravel Tricks That'll Make Your App Fly 🚀
Is your Laravel app slower than a sleepy sloth on a Monday morning? Let's fix that!
Here are 5 performance tricks that are actually simple to implement. No rocket science, I promise!
1. Stop Making Your Database Cry 😭
The Problem: The dreaded N+1 query problem
Imagine asking someone 100 questions one at a time instead of asking all 100 at once. Annoying, right? Your database feels the same way!
Bad way:
// This runs 101 queries! 😱
$posts = Post::all();
foreach ($posts as $post) {
echo $post->author->name; // Each loop hits the database!
}
Good way:
// This runs just 2 queries! 🎉
$posts = Post::with('author')->get();
foreach ($posts as $post) {
echo $post->author->name; // Already loaded!
}
The magic word: with() - It's like meal prepping for your database queries!
2. Cache Everything (Well, Almost) 📦
In production, run these commands and watch your app speed up:
php artisan config:cache
php artisan route:cache
php artisan view:cache
What it does: Laravel stops reading files every single time and just uses the cached version.
When to run it: After deploying. Every. Single. Time.
Pro tip: Remember to clear cache when you change config! (Ask me how I know... 😅)
3. Don't Make Users Wait for Everything ⏰
The scenario: User signs up → You send welcome email → User waits... and waits...
The fix: Use queues!
// Before: User waits for email to send
Mail::to($user)->send(new WelcomeEmail());
// After: Email sends in background, user is happy
Mail::to($user)->queue(new WelcomeEmail());
One word change. Huge difference. That's the Laravel magic! ✨
Just remember: Set up your queue worker (Redis, database, whatever floats your boat)
4. Index Your Database (It's Not a Book, But Still) 📚
Your database without indexes is like trying to find a name in a phone book that's not alphabetized. Painful!
Schema::table('posts', function (Blueprint $table) {
$table->index('user_id'); // Single column
$table->index(['status', 'published_at']); // Multiple columns
});
When to add indexes:
- Columns you search by
- Foreign keys
- Columns in WHERE clauses
Pro tip: Don't go crazy! Too many indexes can slow down writes. Balance, young Padawan!
5. Redis Is Your Friend (And It's Fast) ⚡
File cache is like storing stuff in your garage. Redis is like having a super-organized closet right next to you.
// Cache expensive operations
Cache::remember('popular_posts', 3600, function () {
return Post::orderBy('views', 'desc')->take(10)->get();
});
Translation: Check cache first. If not there, run the query and cache the result for 1 hour.
Real talk: This can turn a 500ms query into a 5ms response. Yeah, it's that good!
Bonus Round: The Quick Wins 🎯
Use select() to fetch only what you need:
// Don't fetch everything if you only need name and email!
User::select('name', 'email')->get();
Use chunk() for big datasets:
// Process 1000 users at a time instead of loading 100k into memory
User::chunk(1000, function ($users) {
// Do stuff with each chunk
});
Use eager loading counts:
// Get post count without loading all posts
$users = User::withCount('posts')->get();
echo $users->first()->posts_count; // No extra query!
The Performance Checklist ✅
Before you deploy:
- Added
with()for relationships (no N+1 queries) - Cached config, routes, and views
- Moved slow tasks to queues
- Added database indexes
- Set up Redis (or at least tried to)
- Tested on real data (not your 3 test users)
Real Talk 💬
Q: "Do I need all of this?"
A: Start with #1 and #2. They're the easiest and give you the biggest wins!
Q: "My app is still slow!"
A: Time to dig deeper! Use Laravel Debugbar or Telescope to find the bottleneck. It's probably a query you forgot to optimize (been there!).
Q: "What about Laravel Octane?"
A: That's the advanced stuff! Get these basics right first, then level up to Octane if you need more speed.
The Bottom Line
Performance optimization is like cleaning your room:
- Start with the biggest mess (N+1 queries)
- Organize what you use often (caching)
- Delegate what you can (queues)
- Make finding stuff easier (indexes)
- Upgrade your storage (Redis)
Your users will thank you with faster load times. Your database will thank you by not dying. Win-win!
Got questions? Drop them on LinkedIn. I've probably made the same mistakes! 😄
Want more Laravel tips? Star this blog repo on GitHub and stay tuned!
Now go make that app ZOOM! 🏎️💨