I agree with the advice in the answer by Rager. Specifying certain controller methods per route, as was done in your first question is a common way to have more succinct methods.
As I explained at the end of this review a blade loop can simplify redundant lines of options. The template in the code above has repetitive lines for the <option>
elements in the select list for the category select list:
<option value="2" @if ($category === '2') selected @endif>Example 1</option> <option value="3" @if ($category === '3') selected @endif>Example 2</option> <option value="4" @if ($category === '4') selected @endif>Example 2</option> <option value="5" @if ($category === '5') selected @endif>Example 3</option> <option value="6" @if ($category === '6') selected @endif>Example 4</option> <option value="7" @if ($category === '7') selected @endif>Example 5</option> <option value="8" @if ($category === '8') selected @endif>Example 6</option>
This is very repetitive and does not comply with the Don't repeat yourself (DRY) principle. It also has two options with "Example 2". Presuming there should only be one option with "Example 2", it could be simplified using a loop with the @selected
directive mentioned in the Additional Attributes section
@for ($i = 2; $i < 9; $i++)
<option value="{{ $i }}" @selected($category == $i)>Example {{ $i - 1 }}</option>
@endfor
The range()
function could also be used with a @foreach
loop:
<option value="0" @selected($category === '0')>All</option>
@foreach (range(2, 8) as $i)
<option value="{{ $i }}" @selected($category == $i)>Example {{ $i - 1 }}</option>
@endfor
- 29.6k
- 16
- 45
- 203