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

// Before Laravel 9:
<select name="country">
<option value="India" {{ old('country') ?? $country == 'India' ? 'selected' : '' }}>India</option>
<option value="Pakistan" {{ old('country') ?? $country == 'Pakistan' ? 'selected' : '' }}>Pakistan</option>
</select>
 
// Laravel 9
<select name="country">
<option value="India" @selected(old('country') ?? $country == 'India')>India</option>
<option value="Pakistan" @selected(old('country') ?? $country == 'Pakistan')>Pakistan</option>
</select>

Tip given by @VijayGoswami

Like our articles?

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

Recent New Courses