mardi 11 août 2015

Why do my laravel 5 checkboxes return checked all the time?

I am creating an array of days in my form that when checked get saved to the db column 'days' in here they are divided with a ',' as an array in a string. I then want to be able to query with these values later on.

The issue I am having is that when I save the form with the checked values they go into the db fine and I can see the array but when I go to edit the form all checkboxes are checked like so:

enter image description here

In my controller store and update methods I have this:

public function store(Offer $offer)
    {

        $data = Input::except('image','thumbnail','featured','days');
        $validation = Validator::make($data, Offer::$rules);
        $days = Input::get('days');

        if ($validation->fails()) {
            return redirect('offers')->with('message', $validation->errors());
        } else {

            $d = implode(',', $days);
            $data['days'] = $d;

            Offer::create( $data );
            return redirect('offers')->with('message', 'Offer added!');
        }

    }

Then in my edit form view I have got this:

<?php 

$days = ['Monday' => 'monday','Tuesday' => 'tuesday','Wednesday' => 'wednesday', 'Thursday' => 'thursday', 'Friday' => 'friday', 'Saturday' => 'saturday', 'Sunday' => 'sunday'];

?>

<div class="form-group" id="featured-form-area">
<h2>Days</h2>
@foreach($days as $day)
    {!! Form::label('day', $day) !!}
    {!! Form::checkbox('days[]', $day, null, ['class' => 'form-field']) !!}
@endforeach
</div>

Can anyone tell me why this would happen? The checkboxes are all checked for some reason.




Aucun commentaire:

Enregistrer un commentaire