From ac4515e1d849206b4f0c4f8666388a1cf247c309 Mon Sep 17 00:00:00 2001 From: xiaomlove Date: Fri, 16 May 2025 03:18:59 +0700 Subject: [PATCH] change TorrentExtra.pt_gen setter/getter --- app/Models/TorrentExtra.php | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/app/Models/TorrentExtra.php b/app/Models/TorrentExtra.php index c6551953..74477536 100644 --- a/app/Models/TorrentExtra.php +++ b/app/Models/TorrentExtra.php @@ -2,6 +2,7 @@ namespace App\Models; +use Illuminate\Database\Eloquent\Casts\Attribute; use Nexus\Database\NexusDB; use Nexus\Torrent\TechnicalInformation; @@ -11,9 +12,23 @@ class TorrentExtra extends NexusModel protected $fillable = ['torrent_id', 'descr', 'ori_descr', 'media_info', 'nfo', 'pt_gen']; - protected $casts = [ - 'pt_gen' => 'array', - ]; + protected function ptGen(): Attribute + { + return Attribute::make( + get: function ($value) { + $jsonDecoded = json_decode($value, true); + if (is_array($jsonDecoded)) { + return $jsonDecoded; + } else { + return $value; + } + }, + set: function ($value) { + return is_array($value) ? json_encode($value) : $value; + } + ); + } + public function torrent() {