Yousef Altaf Yousef Altaf - 10 months ago 48
PHP Question

Laravel : how to check the check-box if it's value is exist in a array

I have an services in an

, and I have a list of
which have the same values in this
no I need to make each
to be
if it's in the

here is my Blade code

{{ Form::checkbox('pro_serves[]', 'pool', null) }} Pool <br/>
{{ Form::checkbox('pro_serves[]', 'gym', null) }} Gym <br/>
{{ Form::checkbox('pro_serves[]', 'maintenance', null) }} Maintenance <br/>
{{ Form::checkbox('pro_serves[]', 'dish', null) }} Dish <br/>
{{ Form::checkbox('pro_serves[]', 'kidsArea', null) }} KidsArea <br/>
{{ Form::checkbox('pro_serves[]', 'parking', null) }} Parking

and here is my Controller

public function editProject($id)
$proId = Projects::findOrFail($id);
$proImg = ProjectsImages::where('image_id', $id)->get();
$proPln = ProjectsPlans::where('image_id', $id)->get();
$services = $proId->pro_serves;
$service = explode(',', $services);

return View::make('admin.manageProject.editProject', compact('proId', 'proImg', 'proPln', 'service'));

now as you see I am sending the
as an
to the
how can I implement this?
and I welcome any new ideas

Answer Source

Assuming you're either using an old version of laravel, or the laravelcollection/html package, you merely need to pass a third argument.

This is a checked checkbox:

{{ Form::checkbox('pro_servers[]', 'pool', true) }}

So, to have it checked if the value is in the array you can simply do:

{{ Form::checkbox('pro_servers[]', 'pool', in_array('pool', $theArray)) }}

There you go.

This is actually covered in the documentation, which I always recommend taking a look at before posting on SO.