user11124425
user11124425

Reputation: 971

How to show old data of select element

I am stuck for 2 days, do you know how to show old data of select element in Laravel?

<select name="sexe" id="sexe" class="form-control">
 <option value="">Choice</option>
 <option>Women</option>
 <option>Man</option>
 </select>

I have tried this but without success:

<select class="form-control" name="sexe">
   <option value="male" @if (old('sexe') == 'male') selected="selected" @endif>male</option>
   <option value="female" @if (old('sexe') == 'female') selected="selected" @endif>female</option>
</select> 

My Controller

public function edit($id)
{
        //
  $candidats = Candidat::find($id);
  $permis = Permis::all();
  return view('admin.candidats.edit', compact('candidats', 'permis'));
}

public function update(Request $request, $id)
{
     $request->validate([
           'sexe' => 'required|string',
           'fk_permis' => 'required'
    ]);

      $candidats = Candidat::find($id);
      $candidats->sexe = $request->get('sexe');
      $candidats->fk_permis = $request->get('fk_permis');
      $candidats->save();
        return redirect()->route('candidats.index')
                    ->with('success', 'mise à jour effectuée');
    }

Edit: 1) index.blade.php

enter image description here

2) edit.blade.php

enter image description here

Upvotes: 0

Views: 162

Answers (2)

Piazzi
Piazzi

Reputation: 2636

In your update function put withInput():

return redirect()->route('candidats.index')
                    ->with('success', 'mise à jour effectuée')->withInput();

In your select you can do this:

<select class="form-control" name="sexe">
   <option value="male" @if (old('sexe') == 'male') selected="selected" @elseif($candidats->sexe == 'male') selected="selected" 
 @endif>male</option>
   <option value="female" @if (old('sexe') == 'female') selected="selected" @elseif($candidats->sexe == 'female') selected="selected" 
 @endif>female</option>
</select> 

I loaded the selected option from your model here

@elseif($candidats->sexe == 'male') selected="selected"

So, if you saved 'male' in your sexe attribute this option will be selected.

Take a look here for more info:

Upvotes: 1

Watercayman
Watercayman

Reputation: 8178

If the old data was stored as a model, which I assume it was since this is a Laravel question and not a javascript question, you can use form-model binding to easily load the old data the next time you go to the page.

So, when you open your form, bind the model:

{{ Form::model($yourModel, array('route' => array('yourModel.update', $yourModel->id))) }}

And then within the select method, laravel (collective) can automatically make the old data the selected value. Using the Laravel helper it might look like this:

{!! Form::select('sexe', $listOfYourNameAndIdForYourSelectItem, null, ['class'=>'form-control', 'id'=>'sexe']) !!}

By making the third argument null, as above, Laravel will make the model's old data the selected element.

Check the docs on the Laravel forms package for more info.

Upvotes: 1

Related Questions