php Laravel:获取复选框的值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41966620/
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-26 02:31:04  来源:igfitidea点击:

Laravel: Get values of checkboxes

phplaravelvalidationcheckbox

提问by hatemjapo

I have multiple checkboxes in the following code:

我在以下代码中有多个复选框:

@foreach($camera_video as $video)
  <input type="checkbox" name="camera_video" value="{{$video->id}}"> <label>{{$video->name}}</label>
@endforeach

I would like to see which checkboxes have been checked by the user. I just need the id (value) to store. What is the best way to do this in Laravel?

我想查看用户选中了哪些复选框。我只需要存储 id(值)。在 Laravel 中执行此操作的最佳方法是什么?

回答by Buglinjo

You should update name of your checkbox input to camera_video[]as array and you are good to go. You will get input array.

您应该将复选框输入的名称更新camera_video[]为数组,然后一切顺利。您将获得输入数组。

<input type="checkbox" name="camera_video[]" value="{{$video->id}}"> <label>{{$video->name}}</label>

If you are using Laravel 5.x.x you should use Request object:

如果你使用 Laravel 5.xx 你应该使用 Request 对象:

public function yourMethod(Request $request)
{
    $cameraVideo = $request->input('camera_video');
    ...
}

回答by Ahmed

You can use

您可以使用

Input::all()

or

或者

Input::get('camera_video')

回答by Imran

You just need to define the name as an array so that you can fetch the input as array in Laravel. Like this:

你只需要将名称定义为一个数组,这样你就可以在 Laravel 中以数组的形式获取输入。像这样:

<input type="checkbox" name="camera_video[]" value="{{$video->id}}"> <label>{{$video->name}}</label>