13

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?

5 Answers 5

32

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');
    ...
}
Sign up to request clarification or add additional context in comments.

5 Comments

i almost got it. in my controller, where i handle the request. I shoul use $camera_video = Input::get('camera_video'); -> the funny thing is, when i write this line i get a Http Error 500 with no hint to any mistake...
What version of Laravel do you use?
i installed a new project, should be 5.4. But i think i have a problem with the storage write permission. Trying to figure it out
If your version is 5 or up you should use Request Object... I'll update my answer
that worked for me. THX. but i still need to figure out why i am getting a 500 server error when i have a mistake in my code.
2

You can use

Input::all()

or

Input::get('camera_video')

Comments

2

you can use has to function on the request object to check the value of the checkbox is exist and is not in the request like below;

public function store(Request $request){
        if($request->has('terms')){
            //Checkbox checked
        }else{
            //Checkbox not checked
        }
}

for multiple;

  public function store(Request $request){
        if(in_array('Mango', $request->get('fruits'))){
            //User like Mango
        }else{
           //User does not like Mango
        }
    }

$request->get('fruits') // this will return the array;

Comments

1

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>

Comments

1

For the multiple checkboxes in a view, you can do it like this.

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

Inside your controller, you can do it like @Buglinjo's answer or else simply use a foreach loop. You will get the value of checked checkboxes only

$camera_videos = $request->input('camera_videos');
foreach ($camera_videos as $camera_video) {
    //you can do whatever you want. I am saving it in MySqli DB
    $table = new Model;
    $table->video_id = $camera_video;
    $table->save();
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.