Laravel: Get values of checkboxes

大兔子大兔子 提交于 2019-12-22 04:47:18

问题


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?


回答1:


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

<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:

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



回答2:


You can use

Input::all()

or

Input::get('camera_video')



回答3:


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

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


来源:https://stackoverflow.com/questions/41966620/laravel-get-values-of-checkboxes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!