Kritim Yantra
Jun 25, 2025
"Laravel makes development joyful — but only if you know its hidden gems!"
After working on multiple real-world Laravel projects, I discovered some tips and best practices that made me say, "I wish I knew this before!" Whether you're a beginner or intermediate developer, these Laravel tips will save you time, reduce bugs, and make your codebase cleaner.
Let’s dive into the Laravel wisdom I wish I had when I started. 🧠
php artisan route:list --compact
The route:list
command is handy — but by default, it's huge.
Use this instead:
php artisan route:list --compact
✅ Cleaner view
✅ Easier to scan
✅ Perfect for APIs
when()
for Clean ConditionalsInstead of writing ugly if
checks everywhere, try this:
return [
'name' => $user->name,
'email' => $this->when($user->isAdmin(), $user->email),
];
✅ Clean
✅ Readable
✅ Ideal in API Resources
withCount()
is a Game-ChangerWant to count related models without looping?
$posts = Post::withCount('comments')->get();
Now you can do:
$post->comments_count
✅ Super fast
✅ No extra queries
tap()
Makes Code BeautifulNeed to modify and return an object in a chain?
$user = tap(new User, function ($user) {
$user->name = 'Ajay';
$user->email = 'ajay@example.com';
$user->save();
});
✅ Cleaner than manually saving
✅ Great for service classes
all()
for APIsIn controllers, never do this:
return User::all(); // 😱
Use pagination:
return User::paginate(10);
✅ Safer
✅ Scalable
✅ API-friendly
FormRequest
for ValidationDon't clutter your controllers with validation.
Instead:
php artisan make:request StorePostRequest
Then in your controller:
public function store(StorePostRequest $request)
{
// Automatically validated
}
✅ Reusable
✅ Testable
✅ Clean
Got logic like this everywhere?
User::creating(function ($user) {
$user->uuid = Str::uuid();
});
Move it to an Observer:
php artisan make:observer UserObserver
✅ Centralizes logic
✅ Easier to debug
✅ Cleaner models
remember()
Instead of manually checking and setting cache:
Cache::remember('posts', 60, function () {
return Post::all();
});
✅ One-liner
✅ Fast
✅ DRY
Resource Collections
with PaginationInstead of:
return PostResource::collection(Post::all());
Do:
return PostResource::collection(Post::paginate(10));
✅ You get metadata
✅ Helps frontend devs
✅ Clean API design
php artisan tinker # Test logic instantly
php artisan queue:work # Run background jobs
php artisan schedule:work # Test scheduler
php artisan migrate:fresh --seed # Fresh start
✅ Speed up dev
✅ Build muscle memory
✅ Feel like a Laravel ninja 🥷
In one real project, I:
User::all()
for dashboardsAfter learning these tips, I:
The result?
⏱️ 5x faster response time
📉 30% lower DB load
👨💻 Easier to maintain and scale
Tip | Benefit |
---|---|
route:list --compact |
Clean route overview |
when() in responses |
Conditional clarity |
withCount() |
Fewer queries |
tap() |
Elegant chaining |
paginate() |
Scalable data |
FormRequest |
Reusable validation |
Observers | Model logic separation |
remember() |
Effortless caching |
Resource Collections | API best practice |
Artisan Commands | Faster development |
Let’s make this a conversation — share your “Laravel tip you wish you knew earlier” in the comments or tweet @ajayyadavexpo. I’ll feature the best ones in the next post!
No comments yet. Be the first to comment!
Please log in to post a comment:
Sign in with Google