Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

mozew's avatar
Level 6

SQLSTATE[01000]: Warning: 1265 Data truncated for column 'infraction_id' at row 1

I did store checkbox array value into table using below's code

public function store(Request $request)
{
    $infraction_id = $request->infraction_id;
    if(is_array($infraction_id))
    {
        $infraction_id = implode(',', $infraction_id);

    }
    $inspection = new Inspection();
    $inspection->infraction_id = $infraction_id;
    $inspection->encouragement_id = 1;
    $inspection->infraction_data = $request->infractions;
    $inspection->encouragement_data = $request->encouragements;
    $inspection->user_id = auth()->user()->id;
    $inspection->requisition_id = $request->input('requisition_id');
    $inspection->save();
    $inspection->requisitions()->update(['status' => 1]);
    return redirect()->route('inspections.index');
}

create.blade.php

<div class="panel-body">
    <div class="table-responsive">
        <table class="table table-bordered">
            @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
        </table>
    </div>
    <div class="text-right">Total:
        <span id="total" class="translate">0</span>
    </div>
</div>

NO 2

0 likes
6 replies
tykus's avatar

If infraction_id is an integer column (and it is), you cannot give it a string

if(is_array($infraction_id))
{
        $infraction_id = implode(',', $infraction_id);
}

If an inspection can have many infractions, then you are missing a many-to-many relation between the Inspection and Infraction models.

1 like
mozew's avatar
Level 6

Ok , How to save it? But I want ids of infractions be on 1 row.

What is the solution?

mozew's avatar
Level 6

What is the solution?

Is it correct?

$inspection->infraction_id = $request->infraction_id;
tykus's avatar

You will need to change the infraction_id column to a string or text type, so you can store a JSON representation of the array of ids. This will break any relations that depend on the infraction_id column

1 like
mozew's avatar
Level 6

I changed the infraction_id column to a text

How to save it now?

tykus's avatar

As you were $inspection->infraction_id = $infraction_id;

You can cast the infraction_id property to an array on the model for convenience

Please or to participate in this conversation.