我有一个 Form::select 下拉列表,并从数据库中的表中填充它。 我想获取选择的索引,因为我想将其保存在数据库中。
这是我的观点
{!! Form::open(["url"=>"devices" ,"files"=>"true"]) !!}
Name : {!! Form::text("device_name") !!}
Category: {!! Form::select('category', $categories) !!}
在 Controller 中我尝试使用key($array)
,但它给了我一个空值:
$device_name = $request->input('device_name');
//$device_category = $request->input('device_category');
$device_category = $request->input('key(category)');
$device_company = $request->input('device_company');
$device_description = $request->input('device_description');
$device_price = $request->input('device_price');
$file= $request->file('image');
$destpath = 'img';
$file_name = $file->getClientOriginalName();
$file->move($destpath,$file_name);
$new_device = new Devices;
$new_device->device_name = $device_name;
$new_device->device_category = $device_category;
$new_device->device_company = $device_company;
$new_device->device_description = $device_description;
$new_device->device_price = $device_price;
$new_device->device_imgURL = "\img\\" . $file_name;
$new_device->save();
我该怎么做?
最佳答案
使用以下命令获取类别数组并在选择框中使用
$categories = Category::pluck('title', 'id');
选择框
{!! Form::select('category', $categories, null, ['class' => 'some-class']) !!}
您可以使用 Controller 中的类别 ID 获取
$request->category
关于php - laravel 从 Select 中获取索引而不是查看值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46454245/