Yousef Altaf Yousef Altaf - 2 months ago 9
PHP Question

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

I have an services in an

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


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
service
as an
array
to the
view
how can I implement this?
and I welcome any new ideas

Answer

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.