I have a table users
with column stime
what i want to achieve - i want the specefic html option <option>
selected selected
when it matches from the database
Like - if stime = 600.0
then this option <option value="600.0">10 minute</option>
will be selected <option value="600.0" selected>10 minute</option>
Laravel Blade Code
<form method="post" action="{{route('setstime2')}}" enctype="multipart/form-data">
@csrf
<div class="boxl" style="margin-left: 10%;">
<h3 class="h31">Set Idle Time</h3>
<select name="select" class="form-select" style="margin-top: 8%;" aria-label="Default select example">
<option value="60.0">1 minute</option>
<option value="120.0">2 minute</option>
<option value="180.0">3 minute</option>
<option value="240.0">4 minute</option>
<option value="300.0">5 minute</option>
<option value="360.0">6 minute</option>
<option value="420.0">7 minute</option>
<option value="480.0">8 minute</option>
<option value="540.0">9 minute</option>
<option value="600.0">10 minute</option>
<option value="660.0">11 minute</option>
<option value="720.0">12 minute</option>
<option value="780.0">13 minute</option>
<option value="840.0">14 minute</option>
<option value="900.0">15 minute</option>
<option value="960.0">16 minute</option>
<option value="1020.0">17 minute</option>
<option value="1080.0">18 minute</option>
<option value="1140.0">19 minute</option>
<option value="1200.0">20 minute</option>
<option value="1200.0">21 minute</option>
<option value="1320.0">22 minute</option>
<option value="1380.0">23 minute</option>
<option value="1440.0">24 minute</option>
<option value="1500.0">25 minute</option>
<option value="1560.0">26 minute</option>
<option value="1620.0">27 minute</option>
<option value="1680.0">28 minute</option>
<option value="1740.0">29 minute</option>
<option value="1800.0">30 minute</option>
</select>
<div style="margin: -10px;"></div>
<button type="submit" class="" style="margin-top: 28px;">Submit</button>
</div>
</form>
CodePudding user response:
You can also do it like this:
<option value="600.0" @if ($user->stime === 600.0) selected @endif >10 minute</option>
CodePudding user response:
You can use a ternary operator to achieve this
<option value="60.0" {{ $user->stime === 60.0 ? 'selected' : ''}}>1 minute</option>
<option value="120.0" {{ $user->stime === 120.0 ? 'selected' : ''}}>2 minutes</option>
...
<option value="1800.0" {{ $user->stime === 1800.0 ? 'selected' : '' }}>30 minutes</option>
If $user
is your logged in user, you can use the auth()
helper or the Auth
facade instead.
<option value="60.0" {{ auth()->user()->stime === 60.0 ? 'selected' : ''}}>1 minute</option>
<option value="60.0" {{ Auth::user()->stime === 60.0 ? 'selected' : ''}}>1 minute</option>
A very compact way of doing this for all options I see in your example (1 to 30 minutes) is using foreach()
, range()
and Str::plural
.
<select name="select" class="form-select" style="margin-top: 8%;" aria-label="Default select example">
@foreach (range (1, 30) as $mtime)
@php($stime = number_format($mtime * 60, 1, '.', ''))
<option value="{{ $stime }}" {{ $user->stime === $stime ? 'selected' : '' }}>
{{ $mtime }} {{ Str::plural('minute', $mtime) }}
</option>
@endforeach
</select>
An @if/@endif
directive can also be used.
<option value="{{ $stime }}" @if($user->stime === $stime) selected @endif>
{{ $mtime }} {{ Str::plural('minute', $mtime) }}
</option>