Adhik Mulat
Adhik Mulat

Reputation: 538

How get old value on select in laravel blade?

i have filter form using select dropdown , but i cant get this old value after i submit this filter like input like type text . someone can help ?

<div class="form-group">
     <label>Status</label>  
        <select class="form-control select2 select2-hidden-accessible" style="width: 100%;" data-select2-id="1" tabindex="-1" aria-hidden="true"
                    name="user_id" id="user_id" required
                    >
                      @foreach($unit as $id => $nama_unit )
                      <option value="{{ $id }}">{{ $nama_unit }}</option>
                      @endforeach
                </select> 
        <div class="help-block with-errors"></div>
    </div>

i add my function controller :

public function search_filter_alkes(Request $request)
{   

    $unit        = User::where('roles_id' , 1)->pluck('nama_unit', 'id'); 
    $user_id = $request->user_id;


    $alat = Alat::with('users')->where('user_id',$user_id)
    ->where('jenis', 'Alkes')->
    get();

    session()->put('user_id',$user_id);

    return view('sarpras.alkes',['user_id' => $user_id , 'unit' => $unit,'alat' => $alat  ])

    ->with('user_id', $user_id)
    ;


}

Upvotes: 0

Views: 13154

Answers (4)

Dwayne Aquan
Dwayne Aquan

Reputation: 1

This worked for me in laravel 8:

<select class="form-control" id="gender" name="gender" required>
   <option value="">Select gender</option>
   <option value="M" @if (old('gender') == "M") {{ 'selected' }} @endif>Male</option>
   <option value="F" @if (old('gender') == "F") {{ 'selected' }} @endif>Female</option>
   <option value="O" @if (old('gender') == "O") {{ 'selected' }} @endif>Other</option>
</select>

Upvotes: -1

Hamelraj
Hamelraj

Reputation: 4826

as per your code use this and make sure you use withInput()

return redirect()->back()->withErrors($validator)->withInput();

<div class="form-group">
     <label>Status</label>  
        <select class="form-control select2 select2-hidden-accessible" style="width: 100%;" data-select2-id="1" tabindex="-1" aria-hidden="true"
                    name="user_id" id="user_id" required
                    >
                      @foreach($unit as $id => $nama_unit )
                      <option value="{{ $id }}" {{ old('user_id') == $id ? "selected" :""}}>{{ $nama_unit }}</option>
                      @endforeach
                </select> 
        <div class="help-block with-errors"></div>
    </div>

EDITED

When you use redirect() after post method you have to use withInput() as i mention above example

return redirect('route')->withInput();

when you use view() you have to pass data like as array like your code then use same variable name

    return view('sarpras.alkes',['user_id' => $user_id , 'unit' => $unit,'alat' => $alat  ])

<option value="{{ $id }}" {{ $user_id == $id ? "selected" :""}}>{{ $nama_unit }}</option>

Upvotes: 3

Shamsheer
Shamsheer

Reputation: 744

You have multiple options:

<select name="tags[]" class="form-control select-tag" multiple>
              @foreach($tags as $tag)
   <option value="{{$tag->id}}" {{in_array($tag->id, old("tags") ?: []) ? "selected": ""}}>{{$tag->name}}</option>
              @endforeach
</select>

OR

<select name="gender" class="form-control" id="gender">
        <option value="">Select Gender</option>
        <option value="M" @if (old('gender') == "M") {{ 'selected' }} @endif>Male</option>
        <option value="F" @if (old('gender') == "F") {{ 'selected' }} @endif>Female</option>
</select>

Upvotes: 0

Nikhil G
Nikhil G

Reputation: 2466

You wil have to compare old value with input key. So replace the below code inside your for loop

@if (Input::old('user_id') == $id)
      <option value="{{ $id }}" selected>{{ $nama_unit }}</option>
@else
      <option value="{{ $id }}">{{ $nama_unit }}</option>
@endif

Upvotes: 1

Related Questions