php - 选择下拉列表的 Laravel 模型绑定(bind)

标签 php laravel-5

我希望在我的编辑表单中选择选择下拉值。

在我的 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/

相关文章:

php - 试图理解 PHP array_map() 调用的奇怪变体

javascript - Ajax 请求和竞争条件(客户端和服务器端)

无论定义什么数据类型,PHP sqlite 都会返回所有字符串

php - 多层评论系统 Laravel

laravel - 什么是 Laravel 特征模式?

PHP/拉拉维尔 : How to save a multidimensional array into the DB

php - Laravel 无法从 crontab 调度作业

php - 如何使用 PDO 指定字符集?

php - PHP和MySQL:mysqli_num_rows()期望参数1为mysqli_result,给定 bool 值

php - 找不到类 'PragmaRX\Tracker\Vendor\Laravel\ServiceProvider'