php 从 Laravel 中的多选表单中获取选定值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15358227/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 09:06:07  来源:igfitidea点击:

Getting selected values from a multiple select form in Laravel

phplaravellaravel-3

提问by SUB0DH

For generating a drop-down list with an item selected by default, the following is done:

为了生成默认选择的项目的下拉列表,执行以下操作:

echo Form::select('size', array('L' => 'Large', 'M' => 'Medium', 'S' => 'Small'), 'S');

So I generated a drop-down list that has more than one item selected by default, in the following way:

所以我生成了一个下拉列表,默认情况下选择了多个项目,如下所示:

echo Form::select('size', array('L' => 'Large', 'M' => 'Medium', 'S' => 'Small'), array('S', 'M'), array('multiple'));

But how do I get the more than one selected values?

但是如何获得多个选定的值呢?

Input::get('size')returns only the last selected string.

Input::get('size')仅返回最后选择的字符串。

回答by MatRt

First, if you want to have multiple item selected by default, you have to give an array of values as 3rd parameter, not a simple value.

首先,如果你想默认选择多个项目,你必须给出一个值数组作为第三个参数,而不是一个简单的值。

Exemple:

例子:

Form::select('size', array('L' => 'Large', 'M' => 'Medium', 'S' => 'Small'), array('S', 'M'), array('multiple'));

should show the select with S and M selected.

应该显示选择了 S 和 M 的选择。

For the second point, you should try to give a name like size[]instead of size, it could be solve the problem (because your posted select is not a simple value, its an array of values)

对于第二点,您应该尝试给出一个名称,size[]而不是size,它可以解决问题(因为您发布的 select 不是一个简单的值,它是一个值数组)

回答by Farveaz

Usual Select statements go

通常的 Select 语句

<select name="select_name" id="select_name" multiple="multiple">

And the workflow is that Laravel gets the form elements by their name. To make it work, change the name to an array.

工作流程是 Laravel 通过名称获取表单元素。要使其工作,请将名称更改为数组。

<select name="select_name[]" id="select_name" multiple="multiple">

This will make laravel get the values of select as an array of data.

这将使 laravel 以数据数组的形式获取 select 的值。

回答by Ismael Viamontes

According with https://laravelcollective.com/docs/5.2/html#drop-down-lists

根据https://laravelcollective.com/docs/5.2/html#drop-down-lists

Form::select('size[]',['L' => 'Large', 'M' => 'Medium', 'S' => 'Small'], ['S', 'M'], ['multiple' => 'multiple', 'class' => 'form-control']);

By the way, please notice dropdown's name (size[]) if you want to be able to use this field as array in your backend.

顺便说一句,如果您希望能够将此字段用作后端中的数组,请注意下拉列表的名称(size[])。

Things get tricky when you want uses relationships as value, for instance

例如,当您希望使用关系作为值时,事情会变得棘手

models
user =>  common fields
size => id, name, slug  [
                         {id : 1 , name : Large, slug : L}, 
                         {id : 2 , name : Small, slug : S},
                         {id : 3 , name : Medium, slug : M}
                         ] 

user_size => id, user_id, size_id  [
                                     {id :1, user_id:1, size_id:1}
                                     {id :2, user_id:1, size_id:3}
                                   ]   

So $user->colorswill return something like

所以$user->colors会返回类似的东西

laravel collection

[
  USER_SIZE => [  'user_id' => 1 , size_id' => 1 ],
  USER_SIZE => [  'user_id' => 1, 'size_id' => 3 ]
]


You could do something like, remember User Modelhave a sizesrelationship of one to many with SIZE Model

你可以做一些类似的事情,记住User Model有一个sizes一对多的关系SIZE Model

Form::select('size[]',['L' => 'Large', 'M' => 'Medium', 'S' => 'Small'], $user->sizes->pluck('size')->pluck('slug')->toArray(), ['multiple' => 'multiple', 'class' => 'form-control']);

Hope it helps

希望能帮助到你

回答by azurecorn

Right word is

正确的词是

pluck

采摘

In controller:

在控制器中:

$skills = Skill::pluck('name', 'id');

$selectedSkills = $user->skills()->pluck('skill_user.id');

In blade:

在刀片中:

Form::select('skills[]', $skills, $selectedSkills, ['class' => 'form-control', 'multiple' => 'multiple']) }}