Data is not getting stored in database for multiple check box in laravel

- 1 answer

Ad

I have created a form, in which I have a list of days to be selected multiple. But those days are not getting stored in database.

here is my controller:

public function store(EventRequest $request)
{
    $input = Request::all();
    $input['days_of_week'] = Input::get('days_of_week');
    Event::create($input);
    return redirect('event');
}

and my view is:

    @foreach($days as $day)
    <ul>
        <li>
            {!! Form::checkbox("days_of_week[]", $day, null) , $day !!}
        </li>
    </ul>
    @endforeach

when I return Input::get('days_of_week'); it displays the check boxes I select.

How can I store checkbox value along with all the other fields present in form.

Ad

Answer

Ad

Like Jerodev said, you cannot store array to the database. What you can do is serialize the array and store it. Then when retrieving the value, you can unserialize it.

public function store(EventRequest $request)
{
    $input = Request::all();
    $input['days_of_week'] = serialize(Input::get('days_of_week'));
    Event::create($input);
    return redirect('event');
}

and you can retrive it like this

$daysOfWeek = unserialize(Event::find(1)->days_of_week);
Ad
source: stackoverflow.com
Ad