How to Get Values of Checkbox Array in Laravel 5

Laravel - get all checkbox values from the Request

This should work:

<input type="checkbox" name="models[]" value="ipad">
<input type="checkbox" name="models[]" value="ipod">

Laravel: Get values of checkboxes

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');
...
}

Laravel 5 retrieve checkbox array value

Setting the third parameter to true in checkbox() method will mark the checkbox as checked. Assuming you have $checked_permission_ids which is an array of all the checked ids, you could do this:

@foreach( $user_permissions as $userpermission )

{!! Form::checkbox('permission_id[]', $userpermission->id, in_array( $userpermission->id, $checked_permission_ids ) ) !!}

@endforeach

Laravel - Store multiple checkbox form values in database

If you have multiple checkboxes like this:

<input type="checkbox" name="services" value="1"/>
<input type="checkbox" name="services" value="2"/>
<input type="checkbox" name="services" value="3"/>

It will send's only one value to server, cause name must be unique. Workaround is to use array inputs:

<input type="checkbox" name="services[]" value="1"/>
<input type="checkbox" name="services[]" value="2"/>
<input type="checkbox" name="services[]" value="3"/>

Then you can grab that input in controller and do something like this:

$services = $request->input('services');
foreach($services as $service){
orders::create($service);
}

Validation of arrays in Laravel:

https://laravel.com/docs/5.7/validation#validating-arrays

More about input arrays:

https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/checkbox#Handling_multiple_checkboxes

How to get form input array into PHP array

Laravel return old input of checkbox array

Change your form to the following-

   <form method = "post" action = "tomyController">
@foreach($ids as $id)
<input type = "checkbox" name = "check[{{$id}}][0]" value = "0"
@if(is_array(old('check['.$id.']')) && (0==old('check['.$id.'][0]'))) checked @endif>
<input type = "checkbox" name = "check[{{$id}}][1]" value = "1"
@if(is_array(old('check['.$id.']')) && (1== old('check['.$id.'][1]'))) checked @endif>
<textarea name = "remarks[{{$id}}]">{{ old('remarks['.$id.']') }}</textarea>
@endforeach
</form>


Related Topics



Leave a reply



Submit