How get old value on select in laravel blade?

Related searches

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)
    ;


}

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>

Define the selected option with the old input in Laravel / Blade , Laravel 6 or above: just use the old() function for instance @if (old('cat')==$cat->id), it will do the rest of the work for you. How its works: select tag store the selected option value into its name attribute in bellow case if you select any option it will store into cat. I was parsing JSON data, and was faced with repopulating the multi-select as an array, and only wanted one table, and wasn’t using keys since this array was being passed back from the show method in the controller, so had to supply the values directly on the template, in my case as a crud resource on the edit.blade.php template (also on the create template).


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

Old Value in Select Option, Hi, I have a form in which there is a select option for Gender, what i want to achive is if my validator fails then its get redirect back with old inp Newest Reply. 1 of 4 replies August 14, 2017. Kaustubh's avatar Kaustubh � Laravel � Kaustubh •. 3 years ago. 12995. 4. Laravel Posted 5 months ago by JoshP ( level 4). Reply. Hi, I have a form in which there is a select option for Gender, what i want to achive is if my validator fails then its get redirect back with old inputs. but i dont know how to populate my select option with old value.


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>

How to show old data of `select` element in Laravel?, Data can be shown in input like this: <input id="name" it doesn't show old value. please help. VIEW <select class="form-control� I have a drop down list for states. I want the list to default on a specific state so the user won't have to select it each time, but I want to be able to get the old value in case they change it.


laravel old value for select option Code Example, “laravel old value for select option” Code Answer. @foreach($services as $service) @if (old('category') == $service->id) <option value="{{ $service->id }}" selected>{{ $service->title }}</option> @else. <option value="{{ $service->id }}">{{ $service->title }}</option> @endif. @endforeach. How to set value in for-loop in Laravel blade. 3 weeks ago. Just compare the value. Do not put it in blade syntax. You shouldn’t mix the two. < select id


The most concise screencasts for the working developer, updated daily. There's no shortage of content at Laracasts. In fact, you could watch nonstop for days upon days, and still not see everything!


Laravel also provides a global old helper. If you are displaying old input within a Blade template , it is more convenient to use the old helper. If no old input exists for the given field, null will be returned: