Laravel Tips I Wish I Knew Earlier (From a Real Project)

Author

Kritim Yantra

Jun 25, 2025

Laravel Tips I Wish I Knew Earlier (From a Real Project)

"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. 🧠


1️⃣ Use 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


2️⃣ Use when() for Clean Conditionals

Instead 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


3️⃣ withCount() is a Game-Changer

Want to count related models without looping?

$posts = Post::withCount('comments')->get();

Now you can do:

$post->comments_count

✅ Super fast
✅ No extra queries


4️⃣ tap() Makes Code Beautiful

Need 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


5️⃣ Avoid Using all() for APIs

In controllers, never do this:

return User::all(); // 😱

Use pagination:

return User::paginate(10);

✅ Safer
✅ Scalable
✅ API-friendly


6️⃣ Use FormRequest for Validation

Don'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


7️⃣ Use Model Observers for Clean Code

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


8️⃣ Cache Smartly with remember()

Instead of manually checking and setting cache:

Cache::remember('posts', 60, function () {
    return Post::all();
});

✅ One-liner
✅ Fast
✅ DRY


9️⃣ Use Resource Collections with Pagination

Instead of:

return PostResource::collection(Post::all());

Do:

return PostResource::collection(Post::paginate(10));

✅ You get metadata
✅ Helps frontend devs
✅ Clean API design


🔟 Artisan Tricks You Should Use Daily

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 🥷


️ Real-World Example: What I Did Wrong (And Fixed)

In one real project, I:

  • ❌ Used User::all() for dashboards
  • ❌ Did validation directly in controllers
  • ❌ Didn’t cache anything
  • ❌ Repeated code across controllers

After learning these tips, I:

  • ✅ Switched to paginated resources
  • ✅ Moved validation to Form Requests
  • ✅ Cached common API responses
  • ✅ Used observers and policies for clean logic

The result?

⏱️ 5x faster response time
📉 30% lower DB load
👨💻 Easier to maintain and scale


✅ Summary: Quick Tip Recap

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

💬 What Laravel Tip Changed Your Life?

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!

Tags

Comments

No comments yet. Be the first to comment!

Please log in to post a comment:

Sign in with Google

Related Posts

What Are Laravel 12 Service Providers?
Web Development
What Are Laravel 12 Service Providers?
Laravel Vue
Kritim Yantra Kritim Yantra
Mar 02, 2025
Laravel 12 New Features And Updates
Web Development
Laravel 12 New Features And Updates
Laravel Php Vue
Kritim Yantra Kritim Yantra
Mar 15, 2025