如何在刀片视图中显示解码的JSON?

时间:2018-11-04 16:09:52

标签: laravel-5.4

我以这种方式链接了表格。

Database

我有四个桌子。 inspectionsrequisitionsinfractionsencouragements

检查迁移表:

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

例如{“ 1”:“ 0”}

1为违规ID表,0为选中或取消选中复选框。我在这里提到了示例:https://www.laracasts.com/discuss/channels/laravel/how-to-store-all-checkboxes-in-laravel

申请表的迁移:

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');
    });
}

我决定从数据库连接表。

connect to database

违规迁移表:

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

infractions

迁移鼓励表:

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();
    });
}

encouragement_data表中有infraction_datainspections字段。然后,我创建了一个表单并将违规和鼓励表单(如JSON)存储在站点https://www.laracasts.com/discuss/channels/laravel/how-to-store-all-checkboxes-in-laravel

现在,我创建一个新视图。用于显示infractions表的JSON标题。我该怎么办?

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

我收到此错误。

  

未定义索引:id

感谢您的回答。

0 个答案:

没有答案