diff --git a/src/Eloquent/DocumentModel.php b/src/Eloquent/DocumentModel.php index 965b1a444..30f4c35b2 100644 --- a/src/Eloquent/DocumentModel.php +++ b/src/Eloquent/DocumentModel.php @@ -266,6 +266,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 +297,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. * diff --git a/tests/Casts/ArrayTest.php b/tests/Casts/ArrayTest.php new file mode 100644 index 000000000..ca45bd296 --- /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', ]; }