Я связал таблицы таким образом.
![Database](https://i.stack.imgur.com/rDFlU.png)
У меня есть четыре стола. 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](https://i.stack.imgur.com/jxusG.png)
Например, {"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](https://i.stack.imgur.com/jxusG.png)
Таблица миграции за нарушения:
public function up()
{
Schema::create('infractions', function (Blueprint $table) {
$table->increments('id');
$table->string('title');
$table->integer('score');
$table->timestamps();
});
}
![infractions](https://i.stack.imgur.com/9mZzU.png)
Таблица миграции для поощрений:
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
Спасибо за ответ.