From 2ef0fa9b6b87c58e3b00daf7639120223a418029 Mon Sep 17 00:00:00 2001 From: Mohammad Mortazavi Date: Sun, 29 Jun 2025 15:21:11 +0330 Subject: [PATCH 1/3] Update DocumentModel.php; --- src/Eloquent/DocumentModel.php | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/src/Eloquent/DocumentModel.php b/src/Eloquent/DocumentModel.php index 965b1a444..c052b37a3 100644 --- a/src/Eloquent/DocumentModel.php +++ b/src/Eloquent/DocumentModel.php @@ -11,6 +11,7 @@ use Illuminate\Contracts\Queue\QueueableCollection; use Illuminate\Contracts\Queue\QueueableEntity; use Illuminate\Contracts\Support\Arrayable; +use Illuminate\Database\Eloquent\Casts\Json; use Illuminate\Database\Eloquent\Concerns\HasAttributes; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\Relation; @@ -266,6 +267,16 @@ public function setAttribute($key, $value) return parent::setAttribute($key, $value); } + /** @inheritdoc */ + protected function isJsonCastable($key) + { + if ($this->hasCast($key, ['array'])) { + return false; + } + + return parent::isJsonCastable($key); + } + /** * @param mixed $value * @@ -287,6 +298,15 @@ protected function asDecimal($value, $decimals) return parent::asDecimal($value, $decimals); } + public function fromJson($value, $asObject = false) + { + if (is_array($value)) { + return $value; + } + + return parent::fromJson($value, $asObject); + } + /** * Change to mongo native for decimal cast. * From 8b08abdf9a8295a6a21a05e4b0e7f310a56aae2a Mon Sep 17 00:00:00 2001 From: Mohammad Mortazavi Date: Sun, 29 Jun 2025 15:21:18 +0330 Subject: [PATCH 2/3] Add test; --- tests/Casts/ArrayTest.php | 44 +++++++++++++++++++++++++++++++++++++++ tests/Models/Casting.php | 2 ++ 2 files changed, 46 insertions(+) create mode 100644 tests/Casts/ArrayTest.php diff --git a/tests/Casts/ArrayTest.php b/tests/Casts/ArrayTest.php new file mode 100644 index 000000000..f7eb319b9 --- /dev/null +++ b/tests/Casts/ArrayTest.php @@ -0,0 +1,44 @@ +create(['arrayValue' => ["Dreamin' 'bout the spot that right now, I'm actually in", 'g-eazy' => 'Still']]); + + self::assertIsArray($model->arrayValue); + self::assertIsArray( + DB::connection() + ->table((new Casting())->getTable()) + ->where('id', $model->id) + ->first()->arrayValue + ); + self::assertEquals(["Dreamin' 'bout the spot that right now, I'm actually in", 'g-eazy' => 'Still'], $model->arrayValue); + + $model->update(['arrayValue' => ['What if I just said, f*ck it, never followed my dreams?']]); + + self::assertIsArray($model->arrayValue); + self::assertIsArray( + DB::connection() + ->table((new Casting())->getTable()) + ->where('id', $model->id) + ->first()->arrayValue + ); + self::assertEquals(['What if I just said, f*ck it, never followed my dreams?'], $model->arrayValue); + } +} diff --git a/tests/Models/Casting.php b/tests/Models/Casting.php index dd2fadce1..fb6ebe494 100644 --- a/tests/Models/Casting.php +++ b/tests/Models/Casting.php @@ -36,6 +36,7 @@ class Casting extends Model 'encryptedArray', 'encryptedObject', 'encryptedCollection', + 'arrayValue', ]; protected $casts = [ @@ -60,5 +61,6 @@ class Casting extends Model 'encryptedArray' => 'encrypted:array', 'encryptedObject' => 'encrypted:object', 'encryptedCollection' => 'encrypted:collection', + 'arrayValue' => 'array', ]; } From a2510db329f3245611eb39241e12b2dfd6b3e160 Mon Sep 17 00:00:00 2001 From: Mohammad Mortazavi Date: Sun, 29 Jun 2025 15:25:32 +0330 Subject: [PATCH 3/3] cs:fix; --- src/Eloquent/DocumentModel.php | 1 - tests/Casts/ArrayTest.php | 4 ++-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/src/Eloquent/DocumentModel.php b/src/Eloquent/DocumentModel.php index c052b37a3..30f4c35b2 100644 --- a/src/Eloquent/DocumentModel.php +++ b/src/Eloquent/DocumentModel.php @@ -11,7 +11,6 @@ use Illuminate\Contracts\Queue\QueueableCollection; use Illuminate\Contracts\Queue\QueueableEntity; use Illuminate\Contracts\Support\Arrayable; -use Illuminate\Database\Eloquent\Casts\Json; use Illuminate\Database\Eloquent\Concerns\HasAttributes; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\Relation; diff --git a/tests/Casts/ArrayTest.php b/tests/Casts/ArrayTest.php index f7eb319b9..ca45bd296 100644 --- a/tests/Casts/ArrayTest.php +++ b/tests/Casts/ArrayTest.php @@ -26,7 +26,7 @@ public function testArray(): void DB::connection() ->table((new Casting())->getTable()) ->where('id', $model->id) - ->first()->arrayValue + ->first()->arrayValue, ); self::assertEquals(["Dreamin' 'bout the spot that right now, I'm actually in", 'g-eazy' => 'Still'], $model->arrayValue); @@ -37,7 +37,7 @@ public function testArray(): void DB::connection() ->table((new Casting())->getTable()) ->where('id', $model->id) - ->first()->arrayValue + ->first()->arrayValue, ); self::assertEquals(['What if I just said, f*ck it, never followed my dreams?'], $model->arrayValue); }