From 6a316f95d51209884f9644eb574b124ac747940c Mon Sep 17 00:00:00 2001 From: Kaspar Rosin Date: Mon, 13 Jun 2022 14:20:26 +0300 Subject: [PATCH 1/2] Update data attribute casting --- src/Models/Media.php | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/Models/Media.php b/src/Models/Media.php index 31474bf..3b6a41a 100644 --- a/src/Models/Media.php +++ b/src/Models/Media.php @@ -4,6 +4,7 @@ use Illuminate\Database\Eloquent\Model; use Illuminate\Support\Facades\Storage; +use Illuminate\Database\Eloquent\Casts\Attribute; use OptimistDigital\MediaField\Classes\MediaHandler; class Media extends Model @@ -86,8 +87,11 @@ public function getFilePathAttribute() return str_replace('public/', '', $this->path) . $this->file_name; } - public function getDataAttribute($value) + protected function data(): Attribute { - return json_decode($value, true); + return Attribute::make( + get: fn ($value) => json_decode($value, true), + set: fn ($value) => is_string($value) ? $value : json_encode($value) + ); } } From baf4440a11697bc71e8d72daa6d47165e069b3ef Mon Sep 17 00:00:00 2001 From: Kaspar Rosin Date: Mon, 13 Jun 2022 14:29:21 +0300 Subject: [PATCH 2/2] Update CHANGELOG.md --- CHANGELOG.md | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 75750c2..821647b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,14 @@ All notable changes to this project will be documented in this file. --- +## [3.0.2] - 13-06-2022 + +### Added + +- Created a custom attribute mutator for `data` column. + - Keeping backwards compatibility. + - Existing `$media->data = json_encode($data)` will stay working. + ## [3.0.1] - 11-03-2022 ### Changed