Replace multi-line if statements with expressive throw_if() and throw_unless() helpers for guard clauses and business validations.
Writing repetitive if ($condition) { throw new CustomException('...'); } checks clutters controller actions and service classes.
Laravel provides the expressive throw_if() and throw_unless() helpers.
Basic Guard Clauses
use App\Exceptions\AccountSuspendedException;
use App\Exceptions\PaymentFailedException;
public function processOrder(User $user, Order $order)
{
// Throws exception if condition evaluates to true
throw_if($user->is_suspended, new AccountSuspendedException('Your account is suspended.'));
// Throws exception if condition evaluates to false
throw_unless($order->is_payable, PaymentFailedException::class, 'This order cannot be paid.');
}
Passing Exception Classes and Messages
You can pass an instantiated exception object or the exception class name with a message:
throw_if(
$subscription->hasExpired(),
SubscriptionExpiredException::class,
'Your subscription expired on ' . $subscription->ends_at->format('M d')
);
Summary
throw_if($bool, $exception, $message)throws when condition is truthy.throw_unless($bool, $exception, $message)throws when condition is falsy.- Simplifies domain guard clauses into single readable lines.
Related Tips
View all tips →Catch Exceptions and Return Fallbacks Cleanly with rescue()
Use the rescue() helper to execute risky operations, catch exceptions, and return fallback values without writing full try-catch blocks.
Create Lightweight CLI Tools with Closure Commands in routes/console.php
Use Artisan::command() in routes/console.php to build focused CLI commands using closures without creating dedicated Command classes.