How to get the values for a series of checkboxes in Laravel 4 controller (if checked)

后端 未结 3 980
既然无缘
既然无缘 2020-12-31 11:56

I would like to get the values for a series of checkboxes I have set up in a Laravel 4 form. Here is the code in the view setting up the checkboxes:

@foreac         


        
相关标签:
3条回答
  • 2020-12-31 12:10

    If checkboxes are related then you should use [] in the name attribute.

    @foreach ($friends as $friend)
    <input tabindex="1" type="checkbox" name="friend[]" id="{{$friend}}" value="{{$friend}}">
    @endforeach
    
    
    $friends_checked = Input::get('friend');
    if(is_array($friends_checked))
    {
       // do stuff with checked friends
    }
    
    0 讨论(0)
  • 2020-12-31 12:23

    Using name="friend[]" in the form field creates an array named friend that is passed to the server, as opposed to name="friend" which passes a string value to the server.

    0 讨论(0)
  • 2020-12-31 12:25

    The array friend must have a key . If there is $friend->id you could try something like this.

     @foreach ($friends as $friend)
      <input tabindex="1" type="checkbox" name="friend[{{$friend->id}}]" id="{{$friend}}">
     @endforeach
    
    0 讨论(0)
提交回复
热议问题