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

mozew's avatar
Level 6

How to store all checkboxes in laravel?

I have 64 Checkbox at my admin panel so only website admin can select or cancel them.

I need to save checkbox that checked at my table like this:

create.blade.php

<input type="hidden" name="requisition_id" value="{{ $requisition->id }}" />
<table class="table table-bordered">
    @foreach($infractions as $infraction)
        <tr>
            <th>{{ $infraction->title }}</th>
            <td>
                <input type="hidden" name="infractions[{{ $infraction->id }}]" value="0" >
                <input type="checkbox" value="1" name="infractions[{{ $infraction->id }}]">
            </td>
        </tr>
    @endforeach
</table>
<br>
<table class="table table-bordered">
    @foreach($encouragements as $encouragement)
        <tr>
            <th class="translate">{{ $encouragement->title }}</th>
            <td>
                <div class="input-group">
                    <input type="text" id="num{{$encouragement->id}}" value="0" class="form-control col-sm-1" name="encouragement[{{ $encouragement->id }}]">
                    <div class="input-group-btn">
                            <input type="button" onclick="incrementValue({{$encouragement->id}})" class="btn btn-warning" value="Add">
                    </div>
                </div>
            </td>
        </tr>
    @endforeach
</table>

InspectionController.php

public function store(Request $request)
{
    $infraction_data="";
    foreach ($request->infractions as $key => $val)
    {
        $infraction_data.=$key.'='.$val.'|';
    }
    $infraction_data=substr($infraction_data,0,strlen($infraction_data)-1);
    $encouragement_data="";
    foreach ($request->encouragement as $key => $val)
    {
        $encouragement_data.=$key.'='.$val.'|';
    }
    $encouragement_data=substr($encouragement_data,0,strlen($encouragement_data)-1);

    $inspection = new Inspection();
    $inspection->infraction_data = $infraction_data;
    $inspection->encouragement_data = $encouragement_data;
    $inspection->user_id = auth()->user()->id;
    $inspection->requisition_id = 1;
    $inspection->save();
}
0 likes
9 replies
lostdreamer_nl's avatar
Level 53

why don't you simply convert it into a json string to save and back into an array when you get it?

Yo ucan do so by either changing the infraction_data and encouragement_data fields from string to json type in your migration or by simply adding 2 mutator methods to the Inspection Model:

public function getInfractionDataAttribute() 
{
    return isset($this->attributes['infraction_data']) 
        ? json_decode($this->attributes['infraction_data'], true)
        : [];
}
public function setInfractionDataAttribute($infractionData) 
{
    $this->attributes['infraction_data'] = json_encode($infractionData);
}

// and do the same for encouragement_data
public function getEncouragementDataAttribute() 
{
    return isset($this->attributes['encouragement_data']) 
        ? json_decode($this->attributes['encouragement_data'], true)
        : [];
}
public function setEncouragementDataAttribute($encouragementData) 
{
    $this->attributes['encouragement_data'] = json_encode($encouragementData);
}

From now on you can simply do this in your controller:

// creating a new row
$inspection = Inspection::create([
    'infraction_data' => $request->infractions,
    'encouragement_data' => $request->encouragement,
    'user_id' => auth()->id(),
    'requisition_id' => 1
]);
// getting the options from some row in the DB

$inspection = Inspection::find(1);
dd( $inspection->encouragement_data );  // shows the whole array
3 likes
mozew's avatar
Level 6

I changed my codes and I tried in 3 hours but I did not succeed

I get this error.

error

Model for Inspection.php

class Inspection extends Model
{
    protected $guarded = [];

    public function getInfractionDataAttribute()
    {
        return isset($this->attributes['infraction_data'])
            ? json_decode($this->attributes['infraction_data'], true)
            : [];
    }

    public function setInfractionDataAttribute($infractionData)
    {
        $this->attributes['infraction_data'] = json_encode($infractionData);
    }

    public function getEncouragementDataAttribute()
    {
        return isset($this->attributes['encouragement_data'])
            ? json_decode($this->attributes['encouragement_data'], true)
            : [];
    }

    public function setEncouragementDataAttribute($encouragementData)
    {
        $this->attributes['encouragement_data'] = json_encode($encouragementData);
    }
}

InspectionController.php

public function store(Request $request)
{
    $inspection = new Inspection();
    $inspection->infraction_data = $request->infractions;
    $inspection->encouragement_data = $request->encouragement;
    $inspection->user_id = auth()->user()->id;
    $inspection->requisition_id = 1;


    $inspection->save();
    //$inspection = Inspection::find(1);
    //dd($inspection->encouragement_data);
}

Thank you if you answer me

mozew's avatar
Level 6
    Schema::create('inspections', function (Blueprint $table) {
        $table->increments('id');
        $table->integer('user_id')->unsigned();
        $table->integer('requisition_id')->unsigned();
        $table->string('encouragement_data')->nullable(true);
        $table->string('infraction_data')->nullable(true);
        $table->timestamps();
        
        $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
        $table->foreign('requisition_id')->references('id')->on('requisitions')->onDelete('cascade');
    });
Cronix's avatar

At some point, you should be able to troubleshoot some of these basic things on your own. data too long for column should be self explanatory, and if you google it you'll get the answer. You're trying to insert too much data into a column.

For instance, let's say you had a field defined as 5 chars max.

$table->string('columnName', 5);

and you tried to insert 6 characters into it, you'll get that error. If you're trying to save a huge json string, use a text type for the database column. Maybe even mediumText depending on how much data it is.

1 like
Cronix's avatar
$table->text('infraction_data')->nullable(true);
1 like
oliver-dvorski's avatar

Yeah tho a string shouldn't cap out that quickly, right? Either way, trying to add a migration to fix this like Cronix suggests is definitely worth a shot.

Of course, if you wanna do this in a non-breaking fashion, you make a new migration, select your table and change the column type

Schema::table('inspections', function ($table) {
    $table->text('infraction_data')->nullable(true)->change();
});
1 like

Please or to participate in this conversation.