Support the ongoing development of Laravel.io →
Laravel Blade

I decided to connect my tables from databases.

connect to database

I have four tables. inspections, requisitions, infractions and encouragements.

Migration for inspections table:

public function up()
{
    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->text('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');
    });
}

inspections

For example {"1":"0"}

1 for id of infractions table and 0 for check or uncheck the checkbox I mentioned the example here: https://www.laracasts.com/discuss/channels/laravel/how-to-store-all-checkboxes-in-laravel

Migration for requisitions table:

public function up()
{
    Schema::create('requisitions', function (Blueprint $table) {
        $table->increments('id');
        $table->integer('school_id')->unsigned();
        $table->integer('user_id')->unsigned();
        $table->integer('inspector_user_id')->unsigned()->nullable();
        $table->integer('type');
        $table->boolean('status');
        $table->boolean('approved');
        $table->timestamps();
		
        $table->foreign('school_id')->references('id')->on('schools')->onDelete('cascade');
        $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
        $table->foreign('inspector_user_id')->references('id')->on('users')->onDelete('cascade');
    });
}

Migration for infractions table:

public function up()
{
    Schema::create('infractions', function (Blueprint $table) {
        $table->increments('id');
        $table->string('title');
        $table->integer('score');
        $table->timestamps();
    });
}

infractions

Migration for encouragements table:

public function up()
{
    Schema::create('encouragements', function (Blueprint $table) {
        $table->increments('id');
        $table->string('title');
        $table->integer('unit_score');
        $table->integer('max_score');
        $table->timestamps();
    });
}

There are encouragement_data and infraction_data fields in inspections table. And I create a form and store the infractions and encouragements form like JSON in site https://www.laracasts.com/discuss/channels/laravel/how-to-store-all-checkboxes-in-laravel

Now I create a new view. for display JSON title of infractions table. How should I do?

InspectionController.php

public function index()
{
    $inspections = Inspection::latest()->whereNotNull('infraction_data')->get();
    return view('Admin.list-violations-school.all', compact('inspections'));
}

inspections.blade.php

@foreach($inspections as $inspection)
    <?php $infraction_data = json_decode(json_encode($inspection->infraction_data),TRUE); ?>
    <tr>
        <td>{{ $infraction_data["id"] }}</td>
        <td>{{ $infraction_data["title"] }}</td>
    </tr>
@endforeach

I get this error.

Undefined index: id

Thanks for answer.

Last updated 3 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

Mahmoud Mahmoud irankhosravi Joined 25 Aug 2017

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2025 Laravel.io - All rights reserved.