Sitemap

5 Hidden Laravel Features That Will Instantly Clean Up Your Code

2 min readJun 3, 2025

If you’re using Laravel regularly, you probably rely on the usual tools like @foreach, count(), or orderBy(). But Laravel actually offers a lot more — tools that are often overlooked but can drastically improve your code quality and readability.

In this post, I’ll show you 5 Laravel features that are underused but incredibly useful. Let’s make your code cleaner, smarter, and a little more Laravel-idiomatic.

1. Replace @foreach with @forelse

Ever written a @foreach and then added an if just to check if the collection is empty? Laravel has a cleaner alternative — @forelse.

Typical @foreach approach:

@if ($products->count() > 0)
@foreach ($products as $item)
<li>{{ $item->name }}</li>
@endforeach
@else
<p>No products found.</p>
@endif

Cleaner with @forelse:

@forelse ($products as $item)
<li>{{ $item->name }}</li>
@empty
<p>No products found.</p>
@endforelse

With @forelse, you handle both data rendering and empty state in one neat block.

2. Use when() in Query Builder for Optional Conditions

Instead of writing conditional if statements to build queries dynamically, you can use when() to make your query more elegant.

Without when():

$query = Product::query();

if (request('category')) {
$query->where('category_id', request('category'));
}

$products = $query->get();

With when():

$products = Product::query()
->when(request('category'), function ($q, $category) {
$q->where('category_id', $category);
})
->get();

It’s readable, concise, and scales better when you have multiple conditional filters.

3. Sort Collections Using orderByDesc()

If you’ve already fetched a collection and need to sort it in descending order by a specific key, don’t requery — just sort it with orderByDesc().

Example:

$products = Product::all();

// Sort by highest sales
$topSelling = $products->orderByDesc('sold_count');

Quick and efficient — no need to hit the database again.

4. Use isEmpty() Instead of count() === 0

Checking for empty collections with count() works, but Laravel collections offer a more expressive and readable way: isEmpty().

Instead of this:

if ($orders->count() === 0) {
echo "No orders yet.";
}

Do this:

if ($orders->isEmpty()) {
echo "No orders yet.";
}

Small change, big readability boost.

5. Filter Relationships with whereRelation() (Laravel 9+)

Filtering data based on related models used to require whereHas() with a closure. Since Laravel 9, there’s a simpler alternative: whereRelation().

Old way:

$orders = Order::whereHas('customer', function ($q) {
$q->where('city', 'Jakarta');
})->get();

New and cleaner:

$orders = Order::whereRelation('customer', 'city', 'Jakarta')->get();

Less boilerplate, and the intent is crystal clear.

Final Thoughts

Laravel’s hidden gems like @forelse, when(), orderByDesc(), isEmpty(), and whereRelation() can take your code from "it works" to "this is clean." They're not just sugar syntax — they help you write more expressive and readable code that other developers (including future you) will appreciate.

If you found this helpful, consider bookmarking it or sharing it with your team. And if you have your own favorite underused Laravel tips, drop them in the comments!

--

--

Developer Awam
Developer Awam

Written by Developer Awam

We share simple and practical web development tutorials using Laravel, Livewire, and modern tools. Built for beginners, loved by everyone.

Responses (6)