samedi 27 janvier 2018

Laravel Blade loop error checkboxes with repeated entried

I'll try and go straight to the problem.

I've got a group of checkboxes which are based on items in my database, it's categories table, basically I'm trying to loop all the existent categories in my database and then check if the current one matches a category already in it, if so, echo out a checked checkbox, if not, just an unchecked checkbox.

Everything seems to work fine until I add more than two categories (which ofcourse are related to articles in the db), at that point more categories than needed are echoed out.

Output:

<div class="form-check form-check-inline">

                                  <input class="form-check-input" type="checkbox" id="category_faccio-quello-che-voglio" value="Faccio quello che voglio" name="categories[]">
                                  <label class="form-check-label" for="category_ faccio-quello-che-voglio">Faccio quello che voglio</label>

                                </div>




                                                                <div class="form-check form-check-inline">

                                  <input class="form-check-input" type="checkbox" id="category_faccio-quello-che-voglio" value="Faccio quello che voglio" name="categories[]">
                                  <label class="form-check-label" for="category_ faccio-quello-che-voglio">Faccio quello che voglio</label>

                                </div>





                                                                <div class="form-check form-check-inline">

                                  <input class="form-check-input" type="checkbox" id="category_informatica-giornaliera" value="Informatica Giornaliera" name="categories[]" checked>
                                  <label class="form-check-label" for="category_ informatica-giornaliera">Informatica Giornaliera</label>

                                </div>




                                                                <div class="form-check form-check-inline">

                                  <input class="form-check-input" type="checkbox" id="category_informatica-giornaliera" value="Informatica Giornaliera" name="categories[]">
                                  <label class="form-check-label" for="category_ informatica-giornaliera">Informatica Giornaliera</label>

                                </div>





                                                                <div class="form-check form-check-inline">

                                  <input class="form-check-input" type="checkbox" id="category_phue9" value="PHUe9" name="categories[]">
                                  <label class="form-check-label" for="category_ phue9">PHUe9</label>

                                </div>




                                                                <div class="form-check form-check-inline">

                                  <input class="form-check-input" type="checkbox" id="category_phue9" value="PHUe9" name="categories[]">
                                  <label class="form-check-label" for="category_ phue9">PHUe9</label>

                                </div>





                                                                <div class="form-check form-check-inline">

                                  <input class="form-check-input" type="checkbox" id="category_j9jam" value="J9JAm" name="categories[]">
                                  <label class="form-check-label" for="category_ j9jam">J9JAm</label>

                                </div>




                                                                <div class="form-check form-check-inline">

                                  <input class="form-check-input" type="checkbox" id="category_j9jam" value="J9JAm" name="categories[]" checked>
                                  <label class="form-check-label" for="category_ j9jam">J9JAm</label>

                                </div>





                                                                <div class="form-check form-check-inline">

                                  <input class="form-check-input" type="checkbox" id="category_8obyw" value="8obyw" name="categories[]">
                                  <label class="form-check-label" for="category_ 8obyw">8obyw</label>

                                </div>




                                                                <div class="form-check form-check-inline">

                                  <input class="form-check-input" type="checkbox" id="category_8obyw" value="8obyw" name="categories[]">
                                  <label class="form-check-label" for="category_ 8obyw">8obyw</label>

                                </div>

As you can see there are double checkboxes, which ofcourse I dont want

My blade file

@foreach($categories as $cat)
                                @if(count($entry->categories) > 0)
                                    @foreach($entry->categories as $catInEntry)

                                @if($catInEntry->slug == $cat->slug)
                                <div class="form-check form-check-inline">

                                  <input class="form-check-input" type="checkbox" id="category_" value="" name="categories[]" checked>
                                  <label class="form-check-label" for="category_ "></label>

                                </div>
                                @else
                                <div class="form-check form-check-inline">

                                  <input class="form-check-input" type="checkbox" id="category_" value="" name="categories[]">
                                  <label class="form-check-label" for="category_ "></label>

                                </div>
                                @endif
                                @endforeach 
                                @else
                                <div class="form-check form-check-inline">

                                  <input class="form-check-input" type="checkbox" id="category_" value="" name="categories[]">
                                  <label class="form-check-label" for="category_ "></label>

                                </div>
                                @endif
                            @endforeach

I had to add a third input checking an if because when there were no categories correlated there was no output, so I kinda fixed it this way

The Controller

public function edit($hash) {
        $entry = Entry::where('hash', $hash)->first();
        $categories = Category::all();
        $tagsInEntry = $entry->tags()->pluck('name');
        $implodedTags = implode('.', $tagsInEntry->toArray());
        $checked = '';
        return view('auth.entries-edit', compact('entry', 'categories', 'tagsInEntry', 'implodedTags', 'checked'));
        //return dd($entry->categories);
    }

I've tried some workarounds but all failed, any suggestion?




Aucun commentaire:

Enregistrer un commentaire