Как отобразить JSON в режиме лезвия? - PullRequest
0 голосов
/ 04 ноября 2018

Я связал таблицы таким образом.

Database

У меня есть четыре стола. inspections, requisitions, infractions и encouragements.

Таблица миграции для проверок:

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 для идентификатора таблицы нарушений и 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();
    });
}

В таблице inspections есть поля encouragement_data и infraction_data. И я создаю форму и сохраняю форму нарушений и поощрений, такую ​​как JSON, на сайте https://www.laracasts.com/discuss/channels/laravel/how-to-store-all-checkboxes-in-laravel

Теперь я создаю новый вид. для отображения JSON заголовок таблицы infractions. Как мне быть?

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

Спасибо за ответ.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...