我有一个Form :: select下拉,并从我的数据库中的表中填充它。 我想得到select的索引,因为我想将它保存在数据库中。
这是我的观点
{!! Form::open(["url"=>"devices" ,"files"=>"true"]) !!}
Name : {!! Form::text("device_name") !!}
Category: {!! Form::select('category', $categories) !!}
在控制器中我尝试使用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();
我该怎么办?
答案 0 :(得分:1)
使用以下内容获取类别数组并在选择框中使用
$categories = Category::pluck('title', 'id');
选择框
{!! Form::select('category', $categories, null, ['class' => 'some-class']) !!}
您可以使用
在控制器中获取类别ID$request->category
答案 1 :(得分:0)
在您的控制器中,您可以按$device_category = $request->input('category');
如果你想设置值,那么你需要传递associatve数组作为第二个参数,否则形成select set defualt index 0,1,2 ......
{{ Form::select('age', [
'young' => 'Under 18',
'adult' => '19 to 30',
'adult2' => 'Over 30']
) }}
这将使用选项值的键。
<select name="age">
<option value="young">Under 18</option>
<option value="adult">19 to 30</option>
<option value="adult2">Over 30</option>
</select>