В основном все, что я помещаю в поле «Дополнительно» ( который представляет собой массив json), он автоматически создает поддельный атрибут для каждого элемента массива. Обычно я не возражаю против этого, однако при сохранении он жалуется, что не может найти эти поддельные атрибуты при сохранении модели.
См. пример модели здесь:
Код: Выделить всё
#attributes: array:13 [▼
"id" => 3
"created_at" => "2023-06-09 04:20:07"
"updated_at" => "2023-06-10 06:01:10"
"name" => "TEST123"
"description" => null
"active_flag" => 1
"read_only_flag" => 0
"extras" => "{"test": "thing", "last_sync": {"time": "Sat, 10 Jun 2023 06:01:09 +0000", "count": 59, "result": "success"}}"
"options" => "{"group_type": "StudentController", "import_users": "1", "student_import_StudentHouse": null, "student_import_StudentCampus": null, "student_import_StudentBoarder": null, "student_import_StudentYearLevel": ["9"], "student_import_StudentStatusDescription": null} ◀"
"integration_id" => null
"test" => "thing"
"last_sync" => array:3 [▼
"time" => "Sat, 10 Jun 2023 06:01:09 +0000"
"count" => 59
"result" => "success"
]
]
Поля «test» и «last_sync» просто элементы в массиве, а не фактические столбцы БД.
Не думаю, что, кроме заполнения массива, я сделал что-то отличное от других моих элементов json.
Я где-то читал, что "дополнительно" - это особый тип столбца, но не помню, где я это видел...
Код: Выделить всё
class Group extends Model
{
use \Backpack\CRUD\app\Models\Traits\CrudTrait;
use HasFactory, HasRoles;
protected $guard_name = 'web';
protected $fillable = ['name', 'description', 'updated_at', 'created_at', 'active_flag', 'extras', 'options','controller'];
protected $casts = [
'extras' => 'array',
'options' => 'array'
];
Код: Выделить всё
$object->extras = array_merge($object->extras ?? [], ['last_sync' => $report]);
$object->save();
Код: Выделить всё
Schema::create('groups', function (Blueprint $table) {
$table->id();
$table->timestamps();
$table->string('name', 50);
$table->string('description', 255)->nullable();
$table->boolean('active_flag')->default(1);
$table->boolean('read_only_flag')->default(0);
$table->json('extras')->nullable();
$table->json('options')->nullable();
$table->string('controller')->nullable();
$table->string('integration_id')->nullable();
});
Спасибо!
Подробнее здесь: https://stackoverflow.com/questions/764 ... tras-field
Мобильная версия