Skip to content
This repository has been archived by the owner on May 11, 2023. It is now read-only.

Commit

Permalink
Merge pull request #58 from outl1ne/feature/data-attribute-casting
Browse files Browse the repository at this point in the history
Feature/data attribute casting
  • Loading branch information
Kaspar Rosin authored Jun 15, 2022
2 parents 0405228 + baf4440 commit 42149ed
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 6 additions & 2 deletions src/Models/Media.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
);
}
}

0 comments on commit 42149ed

Please sign in to comment.