3 Tricks You Can Do in Laravel
Mon, Mar 18, 2024 2:14 AM

After spending countless nights debugging Laravel applications and drinking way too much coffee, I've picked up some pretty useful tricks along the way. Today, I want to share 3 techniques that have genuinely made me a better developer - not just in terms of code, but in solving real problems.

Let me tell you about the time these saved my projects (and my sanity).

Lazy Collections

I remember sitting at my desk, staring at our production server's memory usage graph slowly climbing until it crashed. We were processing monthly user reports for a client, and my code was essentially trying to load their entire user base into memory at once.

My original code looked something like this:

<?php

$users = User::all()->filter(function ($user) {
    return $user->subscribed;
});

Simple, right? Well, it was - until our client's user base grew to 500,000 records. That's when I learned about Lazy Collections:

<?php

$users = User::cursor()->filter(function ($user) {
    return $user->subscribed;
});

The result? Our memory usage dropped from 2GB to just 15MB. I still have the before/after screenshots saved because nobody believed me at first. Sometimes the simplest changes make the biggest impact.

Making Route Model Binding Actually Useful

For the longest time, I was writing the same authorization checks in every controller. It felt wrong, but I couldn't quite put my finger on why until I had a lightbulb moment with route model binding.

Here's what changed everything for me:

<?php

public function resolveRouteBinding($value, $field = null)
{
    return $this->where('slug', $value)
                ->where('organization_id', auth()->user()->organization_id)
                ->firstOrFail();
}

Now my routes are clean, my controllers are slim, and I sleep better at night knowing I'm not going to miss an authorization check somewhere. Plus, when someone new joins the team, they don't have to hunt through controllers to understand our access rules.

Blade Directives That Make Sense

I used to dread touching blade templates. They were filled with nested if statements that looked like they were written by someone having a keyboard fight. Then I learned about custom blade directives, and everything changed.

Here's what I came up with:

<?php

// In my AppServiceProvider
Blade::if('subscribed', function ($plan = null) {
    $user = auth()->user();
    return $user && $user->hasSubscription($plan);
});

Now my templates actually read like English:

<?php

@subscribed('premium')
    <div>Here's your premium content!</div>
@else
    <div>Consider upgrading to see this content</div>
@endsubscribed

The best part? When I handed the project over to another developer, they could actually understand what was going on without needing a decoder ring.

What I Learned From All This

These techniques aren't just about writing better code - they're about solving real problems. Every time I implement one of these patterns, I think about the next developer who'll work on this code. Will they understand it? Will it help them solve problems faster?

I'm still learning new things every day. That's what I love about development - there's always something new to learn.

A Quick Note If you're trying these out for the first time, start small. Maybe try the blade directives first - they're the easiest to implement and hardest to mess up.

What's Next? I'm currently experimenting with some interesting patterns around Laravel's event sourcing. If you're interested in hearing about those experiments (and the inevitable failures along the way), let me know.

Until then, keep coding, keep learning, and remember - we all started somewhere.

P.S. If you found this helpful, consider sharing your own Laravel discoveries. I'm always eager to learn new tricks, especially if they involve making code more maintainable or servers less angry.