[英]How to get all the selected values from a multiselect input?
I am using Laravel and I tried to used die(var_dump('$request->input('users')));
我正在使用 Laravel 并尝试使用die(var_dump('$request->input('users')));
to check the data that are selected using a multiselect input.检查使用多选输入选择的数据。 When I selected all 3 users, the output was just showing the last user id like this当我选择所有 3 个用户时,输出只是显示最后一个用户 ID,如下所示
array(1) {
[0]=>
string(1) "3"
}
This is the input.这是输入。 The name was set as user[0]
because the inputs can be multiple depending on the user how many multiselect input is needed名称设置为user[0]
因为输入可以是多个,这取决于用户需要多少个多选输入
<select class="form-control user" id="user" name="user[0]" multiple="">
@foreach ($users as $user)
@if (old('user') == $user->id)
<option value="{{ $user->id }}" selected="">{{ ucwords($user->name) }}</option>
@endif
@endforeach
</select>
And this below if from the controller下面这个如果来自控制器
$users = array();
$user = $request->input('user');
$user = implode(',', $user);
You should have the input name setup as the syntax for adding an element to an array:您应该将输入名称设置为将元素添加到数组的语法:
name="user[]"
Then you will get your array from $request->input('user', []);
然后你会从$request->input('user', []);
得到你的数组$request->input('user', []);
If you are going to have multiple different selects you can use the index in them if you wish:如果您有多个不同的选择,您可以根据需要在其中使用索引:
name="user[0][]"
name="user[1][]"
You have make your input field as array.您已将输入字段设为数组。
blade.php file刀片.php文件
<select class="form-control user" id="user" name="user[]" multiple="">
@foreach ($users as $user)
@if (old('user') == $user->id)
<option value="{{ $user->id }}" selected="">{{ ucwords($user->name) }}</option>
@endif
@endforeach
</select>
controller控制器
$users = array();
$users = $request->input('user',[]);
$users = implode(',', $users);
Why are you converted array into string here $users = implode(',', $users);
为什么在这里将数组转换为字符串$users = implode(',', $users);
? ?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.