Error when creating an image after updating to v3.6

I updated my project from v3.5 to v3.6 and now this code fails:

$record->image()->create([‘data’ => $this->path . $image]);

With this error:

SQLSTATE[HY000]: General error: 1364 Field ‘disk_name’ doesn’t have a default value (SQL: insert into system_files (is_public, field, attachment_id, attachment_type, updated_at, created_at) values (1, image, 1, Utopigs\Banners\Models\BannerItem, 2024-03-08 16:46:38, 2024-03-08 16:46:38))

It works if I change the line to:

$record->image = (new \System\Models\File)->fromFile($this->path . $image);
$record->save();

But we are using this code in dozens of places where we import demo data for our plugins. Is this a bug that can be fixed in October, or will we have to change our code everywhere?

1 Like

Hey @Maria_UtopigStudio

This approach has been replaced with the fromFile method which is more secure. The create([]) method is assumed to be safe for user input, and this is a big problem if a developer allowed it.

Check the documentation for usage of fromFile:

Eg:

$image = $record->image()->make();
$image->fromFile($this->path . $image);
$image->save();
1 Like

Ok, I understand, I’m going to replace it then!

1 Like