Yassin
Yassin

Reputation: 89

show old data of `select` element in blade laravel 7

That's code showing deliverer names and if one selected I got his id on db table

<select name="deliverer_id" class="selectpicker form-control">
                                @foreach($dvs as $dv)

                                    <option value="{{$dv->id}}"> {{ $dv->name }} </option>


                                @endforeach

                            </select>

I wanna show the old name of deliverer_id. I try before @if ...

Upvotes: 3

Views: 341

Answers (3)

Yassin
Yassin

Reputation: 89

I foud the solution

@foreach($dvs as $dv)

           <option value="{{ $dv->id}}"{{ old('deliverer_id', $order->deliverer_id) == $dv->id ? 'selected' : '' }}   > {{$dv->name}} 
                         </option>

 @endforeach

Upvotes: 1

Mahmood Ahmad
Mahmood Ahmad

Reputation: 494

I'm not sure what you mean but it seems that you want to select a default option based on specific condition in that case you can do as in the following:

<select name="deliverer_id" class="selectpicker form-control">
   @foreach($dvs as $dv)
       <option value="{{$dv->id}}" {{$dv->id == $old_deliverer_id ?? "selected" : ""}}> {{ $dv->name }}</option>
   @endforeach
</select>

Upvotes: 0

ahappydev
ahappydev

Reputation: 103

You will need to compare your value in foreach loop with the old submitted value. For that Laravel provides a method to retreive old value from request $request->old('deliverer_id');

Or you can do it in your blade like this

<select name="deliverer_id" class="selectpicker form-control">
    @foreach($dvs as $dv)
      <option value="{{$dv->id}}" @if($dv->id == old('deliverer_id')) selected @endif > {{ $dv->name }} </option>
    @endforeach
</select>

Upvotes: 1

Related Questions