Selected blade directive

In Laravel 9, you'll be able to use the cool new "selected" Blade directive for HTML select elements.

This is going to be a nice addition that we can use to clean up our Blade views a little bit

1// Before Laravel 9:
2<select name="country">
3 <option value="India" {{ old('country') ?? $country == 'India' ? 'selected' : '' }}>India</option>
4 <option value="Pakistan" {{ old('country') ?? $country == 'Pakistan' ? 'selected' : '' }}>Pakistan</option>
5</select>
6 
7// Laravel 9
8<select name="country">
9 <option value="India" @selected(old('country') ?? $country == 'India')>India</option>
10 <option value="Pakistan" @selected(old('country') ?? $country == 'Pakistan')>Pakistan</option>
11</select>

Tip given by @VijayGoswami

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