SQLSTATE[22007]: Invalid datetime format: 1366 Incorrect integer value: '["1","3","66","68"]' for column 'infraction_id' at row 1

I am trying to save the entire data, but I need to array, and data format error occurs.

blade

@foreach($infractions as $infraction)
    <tr>
        <th>{{ $infraction->title }}</th>
        <td>
            <input type="checkbox" name="infraction_id[]" value="{{ $infraction->id }}" data-toggle="toggle" data-on="Yes" data-off="No" data-onstyle="success" data-offstyle="danger" data-score="{{ $infraction->score }}">
        </td>
    </tr>
@endforeach

Controller

public function store(Request $request)
{
    $inspection = new Inspection();
    $inspection->infraction_id = $request->infraction_id;
    $inspection->encouragement_id = 1;
    $inspection->user_id = auth()->user()->id;
    $inspection->requisition_id = $request->input('requisition_id');
    $inspection->save();
    $inspection->requisitions()->update(['status' => 1]);
    return redirect()->route('inspection-results.index');
}

I see this error

SQLSTATE[22007]: Invalid datetime format: 1366 Incorrect integer value: '["1","3","66","68"]' for column 'infraction_id' at row 1 (SQL: insert into inspections (infraction_id, encouragement_id, user_id, requisition_id, updated_at, created_at) values (["1","3","66","68"], 1, 1, 1, 2018-11-08 10:40:45, 2018-11-08 10:40:45))

array

1 answer

  • answered 2018-11-08 07:41 Nigel Ren

    As you are getting n array of values for infraction_id (when you define it in your HTML you use name="infraction_id[]") You will need to add multiple records...

    foreach ( $request->infraction_id as $infraction_id ) {
        $inspection = new Inspection();
        $inspection->infraction_id = (int)$infraction_id;
        $inspection->encouragement_id = 1;
        $inspection->user_id = auth()->user()->id;
        $inspection->requisition_id = $request->input('requisition_id');
        $inspection->save();
        $inspection->requisitions()->update(['status' => 1]);
    }
    return redirect()->route('inspection-results.index');