lundi 20 janvier 2020

How to store and update multiple selected checkboxes

In the Transfer Model

protected $enumDays = [
        '1' => 'Sunday',
        '2' => 'Monday',
        '3' => 'Tuesday',
        '4' => 'Wednesday',
        '5' => 'Thursday',
        '6' => 'Friday',
        '7' => 'Saturday'
    ];

In TransferController

public function store(Request $request)
    {

        request()->validate([
            'office_id' => 'required',
            'from_destination_id' => 'required',
            'to_destination_id' => 'required',
            'forAgency_id' => 'required',
            'type' => 'required',
            'periodStart' => 'required',
            'periodEnd' => 'required',
            'days' => 'required',
            'vehicle_id' => 'required',
            'cost_round_trip' => 'required',
            'cost_one_way' => 'required',
            'addedBy_user_id' => 'required',
            'status' => 'required',
        ]);

        try {
            Transfer::create($request->all());
        } catch (QueryException $exception) {
            return back()->withErrors($exception)->withInput();
        }

        return redirect()->route('transfers.index')
            ->with('success','Transfer created successfully.');

In create.blade.php

<div class="col-xs-12 col-sm-12 col-md-12">
            <div class="form-group">
                <strong>Days:</strong>
                @foreach(\App\Transfer::getEnum('day') as $day)
                    {!! Form::label($day) !!}
                    {!! Form::checkbox('days', null, false, ['multiple'=>'multiple']) !!}
                @endforeach
            </div>
        </div>

I need to store and update with multiple days selection

I used implode(',',$days) and i got an error : array to string conversion

please help :)




Aucun commentaire:

Enregistrer un commentaire