1

I'm facing a problem that all checkboxes are checked. I want only the checkbox which has a value in database to be checked.

I have these three tables where place_category is the pivot table 
-------  --------------   ----------
places  place_category    categories 
------  --------------    ----------
id         place_id           id
name       category_id        name

eloquent relationships are set.
Here I'm trying to associate many categories for one place but I want only the categories that I added to that place to be checked in the checkbox.
...........

 @foreach($categories as $category)
    <div class="col-lg-3">
         <div class="form-group">

           <input type="checkbox" name="category[]" value="{{$category->id}}" {{isset($category) ? 'checked': '' }} >{{ $category->name }}<br>
                                        </div>
                                </div>

 @endforeach

Is there any problem with my condition ? {{isset($category) ? 'checked': '' }}

3
  • are you passing this Place to the view? Commented Nov 14, 2019 at 6:47
  • yes to Show.blade.php Commented Nov 14, 2019 at 6:52
  • ' public function show($id) { $categories = Category::all(); $place = Place::findOrFail($id); return view('Admin.places.show',['place'=>$place,'categories'=>$categories]); }' Commented Nov 14, 2019 at 6:53

3 Answers 3

1

The problem is that your $category will always be set this way, it will always return true.

You are probably iterating over all the categories, so you need to compare the category with for example the product or whatever you have in the view, and check if the category id is the one selected for that product.. something like this:

@foreach($categories as $category)
<div class="col-lg-3">
    <div class="form-group">
        <input type="checkbox" name="category[]"
           value="{{$category->id}}"
           @if($category->id === $model->category_id) 'checked' @endif>
           {{ $category->name }}<br>
    </div>
</div>
@endforeach

Now this code will check the category only if it was attached to the model that you are listing. Show more code from the controller maybe and the view to get better help if this is not sufficient for you.

-- EDIT

Based on your edit, you are using a many to many relationship there.. so this will do it:

@if($place->categories->contains($category->id)) 'checked' @endif
Sign up to request clarification or add additional context in comments.

3 Comments

I updated my question. how can I compare it according to my data? @nakov
@Developer okay so you have many to many relationship there. Can you edit again and give your controller code please?
@Developer I edited my answer with what you should use, make sure you have categories method within your Place model.
0

You can use empty()

{{ !empty($category) ? 'checked': '' }}

Comments

0

You can see if the Collection of Categories the Place has, $place->categories, contains the current Category being iterated.

@foreach($categories as $category)
    <div class="col-lg-3">
        <div class="form-group">
            <input type="checkbox"
                name="category[]" value="{{$category->id}}"

                {{ $place->categories->contains($category) ? 'checked' : '' }}

                >{{ $category->name }}<br>
        </div>
    </div>
@endforeach

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.