php - laravel 从 Select 中获取索引而不是查看值

标签 php laravel

我有一个 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/

相关文章:

php - 通过以太网安全地将数据从 Arduino 发送到 MySQL 数据库

php - Laravel 中 $errors 的含义是什么

php - 为什么我无法使用 Laravel 将带有用户 ID 字段的帖子保存到数据库?

php - 如何使用 Laravel Passport Scopes + Password Grant Type 限制用户操作

javascript - 如何使用 JQuery 和 formData 正确获取表单数据

php - 对一列的数组数据进行分组并对另一列的数据求和

php - 为 PHP 网络应用程序选择正确的 OAuth2 授权类型

php - 是否可以更改 PHP 错误日志输出?

php - Laravel eloquent "with"和 "where"具有多个表

angularjs - 身份验证中的 Laravel Angular CORS 问题