简体   繁体   English

如何仅检查 laravel 数据库中存储的复选框值?

[英]How to check only stored checkbox value in database in laravel?

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)? {{isset($category)? 'checked': '' }} '检查':'' }}

The problem is that your $category will always be set this way, it will always return true.问题是您的$category将始终以这种方式设置,它将始终返回 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:您可能正在遍历所有类别,因此您需要将类别与例如产品或视图中的任何内容进行比较,并检查类别 ID 是否是为该产品选择的类别 .. 像这样:

@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.现在,此代码仅在附加到您列出的 model 时才检查类别。 Show more code from the controller maybe and the view to get better help if this is not sufficient for you.显示更多来自 controller 的代码,如果这对您来说还不够,可以查看以获得更好的帮助。

-- 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

You can use empty()您可以使用空()

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

Try.尝试。 @if($category->id === $model->category_id) 'checked' @endif

@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

You can see if the Collection of Categories the Place has, $place->categories , contains the current Category being iterated.您可以查看 Place 具有的类别集合$place->categories是否包含正在迭代的当前类别。

@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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM