Skip to content

Commit f9af068

Browse files
authored
Merge pull request #15 from COCOUVI/main
Add Blade tip for the @selected directive
2 parents e990f65 + 51e1c8d commit f9af068

1 file changed

Lines changed: 12 additions & 0 deletions

File tree

tips/views.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
- [Type Hinting for Blade](#laravel-tip--type-hinting-for-blade-️)
44
- [The "checked" Blade Directive](#laravel-tip--the-checked-blade-directive-️)
5+
- [The "selected" Blade Directive](#laravel-tip--the-selected-blade-directive-️)
56
- [Access the Parent Loop Variable](#laravel-tip--access-the-parent-loop-variable-️)
67
- [Short Attribute Syntax](#laravel-tip--short-attribute-syntax-️)
78
- [Blade To HTML](#laravel-tip--blade-to-html-️)
@@ -39,6 +40,17 @@ Often, we need to conditionally mark an input as checked. While this can be done
3940
@checked(old('active', $user->active)) />
4041
```
4142

43+
## Laravel Tip 💡: The "selected" Blade Directive ([⬆️](#views--blade-tips-cd-))
44+
45+
When working with select elements, you might need to conditionally mark an option as selected. While you can do this manually, Laravel ships with the "selected" directive to do exactly that 🚀
46+
47+
```diff
48+
- <option value="admin" {{ $user->role === 'admin' ? 'selected' : '' }}>
49+
+ <option value="admin" @selected($user->role === 'admin')>
50+
Admin
51+
</option>
52+
```
53+
4254
## Laravel Tip 💡: Access the Parent Loop Variable ([⬆️](#views--blade-tips-cd-))
4355

4456
Sometimes, when dealing with nested loops, you may want to keep track of the parent's iteration. Blade makes it incredibly easy, as you have access to the parent loop variable 🚀

0 commit comments

Comments
 (0)