Skip to content

Commit 51e1c8d

Browse files
committed
rephrase to match other tips
1 parent d02db2b commit 51e1c8d

1 file changed

Lines changed: 5 additions & 26 deletions

File tree

tips/views.md

Lines changed: 5 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -42,34 +42,13 @@ Often, we need to conditionally mark an input as checked. While this can be done
4242

4343
## Laravel Tip 💡: The "selected" Blade Directive ([⬆️](#views--blade-tips-cd-))
4444

45-
When working with `<select>` elements, you often need to conditionally mark an `<option>` as selected.
46-
While you can do this manually with a ternary expression, Laravel provides the `@selected` Blade directive 🚀
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 🚀
4746

48-
```blade
49-
{{-- Without @selected (cluttered) --}}
50-
<option value="admin" {{ $user->role === 'admin' ? 'selected' : '' }}>
47+
```diff
48+
- <option value="admin" {{ $user->role === 'admin' ? 'selected' : '' }}>
49+
+ <option value="admin" @selected($user->role === 'admin')>
5150
Admin
52-
</option>
53-
```
54-
55-
```blade
56-
{{-- With @selected (cleaner) --}}
57-
<option value="admin" @selected($user->role === 'admin')>
58-
Admin
59-
</option>
60-
```
61-
62-
The `@selected` directive will automatically add the `selected` attribute when the given condition is `true`.
63-
This keeps your Blade templates cleaner and easier to read.
64-
65-
```blade
66-
<select name="role">
67-
@foreach ($roles as $role)
68-
<option value="{{ $role->value }}" @selected($user->role === $role->value)>
69-
{{ $role->label }}
70-
</option>
71-
@endforeach
72-
</select>
51+
</option>
7352
```
7453

7554
## Laravel Tip 💡: Access the Parent Loop Variable ([⬆️](#views--blade-tips-cd-))

0 commit comments

Comments
 (0)