Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

david001's avatar

checkbox

My all checkbox are checked when displayed in view,but it is wrong ,only that chekbox which value are in database should be checked ,if its value is not present then checkbox should not be checked

controller:

public function index(){

        $properties = \DB::table('property')->get();
        return view('property.building.form',compact('properties'));
    }

public function create(Request $request)
    {
        $topics = Input::except('_token');
        $topics['topics'] = implode(',' , Input::get('topics'));
        \DB::table('property')->insert($topics);
        return"ok";
    }

form:

<form action="{{url('property/create')}}" method="POST">

               <input type="hidden" name="_token" value="{{ csrf_token() }}">               <div class="row t-sec">
                    <div class="col-md-6 l-sec">
                        <div class="information-box">
                            <h3>Basic Details <i class="fa fa-info"></i></h3>

                            <div class="box-content">
                                <div class="field-row">
                                    <input type="text" name="title" placeholder="Title" id="p-title">
                                </div>
                                    <li class="col-xs-6 col-sm-4 hsq-checkbox">
                                        <label for="p-f-11">
                                            <input type="checkbox" value="1" id="p-f-11">
                                            <span></span>
                                            Physics
                                        </label>
                                    </li>
                                    <li class="col-xs-6 col-sm-4 hsq-checkbox">
                                        <label for="p-f-12">
                                            <input type="checkbox" value="2" name="topics[]"id="p-f-12">
                                            <span></span>
                                        chemistry
                                        </label>
                                    </li>
                                    <li class="col-xs-6 col-sm-4 hsq-checkbox">
                                        <label for="p-f-13">
                                            <input type="checkbox" value="3"name="topics[]" id="p-f-13">
                                            <span></span>
                                            Biology
                                        </label>
                                    </li>
                                    <li class="col-xs-6 col-sm-4 hsq-checkbox">
                                        <label for="p-f-14">
                                            <input type="checkbox" value="4" name="topics[]" id="p-f-14">
                                            <span></span>
                                            Mathematics
                                        </label>
                                    </li>
                                
                                </ul>
                            </div>
                        </div>
                    </div>
                </div>
                <button type="submit" class="btn btn-primary">Submit</button>
            </form>
        </div>
    </section>

view:

    <section class="main-container container">
    <?php 
    $all_data = array();
    ?>
    @foreach($properties as $property)

     <?php
                $all_data[] =  $property->id;
    ?>
    
 <label class="form-checkbox form-normal form-green form-text">
           
 <li>{{ Form::checkbox('topics[]', $property->id, in_array($property->id, $all_data)) }}Physics</li>
 <li>{{ Form::checkbox('topics[]', $property->id, in_array($property->id, $all_data)) }}Chemistry</li>
 <li>{{ Form::checkbox('topics[]', $property->id, in_array($property->id, $all_data)) }}Biology</li>
 <li>{{ Form::checkbox('topics[]', $property->id, in_array($property->id, $all_data)) }}Mathematics</li>


    {{ Form::label('title', $property->title) }}<br>
           </label>

    @endforeach
    




In database i have value stored like  this  ```1,2,3,4```        ```  1,2```             ``` 1 ,4```     etc
in view my all checkbox are checked ,i want to check that checkbox which value are present in database not all.How can i do this

0 likes
3 replies
david001's avatar

i looked that but in my case it is for multiple checkbox.anyone thanks

Please or to participate in this conversation.