Set conditional object properties

You can use the when() or unless() methods in your MailMessage notifications to set conditional object properties like a call to action.

1class InvoicePaid extends Notification
2{
3 public function toMail(User $user)
4 {
5 return (new MailMessage)
6 ->success()
7 ->line('We've received your payment)
8 ->when($user->isOnMonthlyPaymentPlan(), function (MailMessage $message) {
9 $message->action('Save 20% by paying yearly', route('account.billing'));
10 })
11 ->line('Thank you for using Unlock.sh');
12 }
13}

Use the when or unless methods in you own classes by using the Illuminate\Support\Traits\Conditionable trait

Tip given by @Philo01

Like our articles?

Become a Premium Member for $129/year or $29/month
What else you will get:
  • 22 courses (477 lessons, total 38 h 20 min)
  • 2 long-form tutorials (one new every week)
  • access to project repositories
  • access to private Discord