logo
podcast Podcast
get help Get Unstuck

Changing your mind about when() and unless() in Eloquent

Revisiting old habits as Laravel evolves

Joel Clermont
Joel Clermont
2025-08-11

As developers, we have the freedom to change our mind on how to write code over time. We aren't locked into a single way of doing things forever.

In the latest episode of the No Compromises podcast, Aaron talks about how he used to wrap every query in plain old if-statements, until Laravel’s when()/unless() helpers (and arrow functions) won him over.

We discuss readability trade-offs, share guidelines for deciding which style to use, and how tools such as Rector can automate the switch.

  • 00:00 Refining long-held opinions
  • 00:45 Aaron's original "query-then-if" pattern
  • 01:45 Why when() first felt clumsy (closures, scopes, extra params)
  • 03:45 Arrow functions & smaller conditions make when() nicer
  • 05:00 Joel's lingering objection: avoiding unless() for readability
  • 06:45 Seeing the same helper everywhere changes minds
  • 08:30 Keep revisiting old habits as Laravel evolves
  • 09:30 Silly bit

Listen Now (11 min)

And after listening, don't forget to subscribe to the podcast, so you don't miss future episodes.

Here to help,

Joel

P.S. I've mentioned Rector in many different scenarios, but I still rarely see teams using it. Would you like help introducing it to your project?

Toss a coin in the jar if you found this helpful.
Want a tip like this in your inbox every weekday? Sign up below 👇🏼
email
No spam. Only real-world advice you can use.