mercredi 21 septembre 2016

saving check box values to the database in laravel

I'm new to laravel. I'm saving some checkbox value to the database using loop the loop work but it only saves the last value in the array to the database.

this is my form

<form action="" method="post" enctype="multipart/form-data">
            <input hidden name="h_id" value="">
            @foreach($facility as $facilities)
            <div class="col-md-4">
                <img src="" width="50px" height="50px;">
                <label></label>
                <input type="checkbox" value="" name="facilities[]">
            </div>
            @endforeach
            <div class="row">
                <input type="hidden" name="_token" value="">
                <input type="submit" class="btn btn-success" value="Next">
                <input type="reset" class="btn btn-success" value="Reset">
            </div>

        </form>

form working fine; $facilities and $hotel are passed from the controller.

this is the store function

public function store(Request $request) {
    $resortfacility = new Resortfacility;
    $loop = $request->get('facilities'); 

    foreach ($loop as $value){
        $resortfacility->h_id = $request->get('h_id');
        $resortfacility->f_id = $value;

        $resortfacility->save();

    }
}

is there any other way to do this that works?




Aucun commentaire:

Enregistrer un commentaire