我希望在我的编辑表单中选择选择下拉值。
在我的 Controller 中
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'));
}
在 View 中
{{ 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
属性,如果它是这样选择的:
{{ 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>
关于php - 选择下拉列表的 Laravel 模型绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47011544/