Simplify if on a request with whenFilled() helper

We often write if statements to check if a value is present on a request or not.

You can simplify it with the whenFilled() helper.

1public function store(Request $request)
2{
3 $request->whenFilled('status', function (string $status)) {
4 // Do something amazing with the status here!
5 }, function () {
6 // This it called when status is not filled
7 });
8}

Tip given by @mmartin_joo

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