本文介绍了如何显示选择元素的旧数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我被困了2天,您知道如何在Laravel中显示select元素的旧数据吗?
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>
我尝试过但没有成功:
<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>
我的控制器
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');
}
1) index.blade.php
2) edit.blade.php
推荐答案
在您的更新函数中,放入withInput()
:
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"
因此,如果您在"sexe
"属性中保存了男性",则将选中此选项.
So, if you saved 'male' in your sexe
attribute this option will be selected.
此处以了解更多信息:
这篇关于如何显示选择元素的旧数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!