用于选择下拉列表的Laravel模型绑定 [英] Laravel model binding for select dropdown
本文介绍了用于选择下拉列表的Laravel模型绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我希望在编辑表单中选择选择下拉列表值.
I want the select dropdown value to be selected in my edit form.
在我的控制器中
public function edit($id)
{
$vedit = DB::table('vehicles')->where('id', $id)->first();
$cartype= DB::table('car_category')->pluck('cartype');
return view('vehicles.edit', compact('vedit','cartype'));
}
可见
{{ Form::label('Vehicle Type', 'Vehicle Type') }}
<select name="vehicle_type" class="form-control">
@foreach($cartype as $cartypes)
<option value="{{ $cartypes}}">{{ $cartypes}}</option>
@endforeach
</select>
我该如何实现?
推荐答案
如果是这样选择的属性,则可以添加selected
属性:
You can add selected
attribute if it's the choosen one like this :
{{ Form::label('Vehicle Type', 'Vehicle Type') }}
<select name="vehicle_type" class="form-control">
@foreach($cartype as $cartypes)
<option value="{{ $cartypes}}" {{ $cartypes == $vedit->vehicle_type ? 'selected' : ''}}>{{ $cartypes}}</option>
@endforeach
</select>
这篇关于用于选择下拉列表的Laravel模型绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文