From 7dfa6f5a40344e4ee53f7c9d1c8d94f27f0faf29 Mon Sep 17 00:00:00 2001 From: Gerard Lamusse Date: Wed, 24 Mar 2021 11:14:12 +0100 Subject: [PATCH 1/6] Ensure attribute is Collection when casting even if value is null Maintains `null` value in database if the value is still empty and the original value was `null` When casting attribute to `asCollection` you can now start using it without checking if it is set or not. --- .../Database/Eloquent/Casts/AsCollection.php | 24 ++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/src/Illuminate/Database/Eloquent/Casts/AsCollection.php b/src/Illuminate/Database/Eloquent/Casts/AsCollection.php index c2d567b504f7..e031045a533e 100644 --- a/src/Illuminate/Database/Eloquent/Casts/AsCollection.php +++ b/src/Illuminate/Database/Eloquent/Casts/AsCollection.php @@ -19,13 +19,35 @@ public static function castUsing(array $arguments) return new class implements CastsAttributes { public function get($model, $key, $value, $attributes) { - return isset($attributes[$key]) ? new Collection(json_decode($attributes[$key], true)) : null; + return new Collection(json_decode($attributes[$key] ?? '[]', true)); } public function set($model, $key, $value, $attributes) { + if ($this->isEmpty($value) && $this->wasEmpty($model, $key)) { + return [$key => null]; + } + return [$key => json_encode($value)]; } + + protected function isEmpty($value) + { + if ($value instanceof Collection) { + return $value->isEmpty(); + } + + return ! $value; + } + + protected function wasEmpty($model, $key) + { + if ($model instanceof Model) { + return $model->getRawOriginal($key) === null; + } + + return false; + } }; } } From af2e836c832861a89fe18e7381b9cb34fee21d21 Mon Sep 17 00:00:00 2001 From: Gerard Lamusse Date: Wed, 24 Mar 2021 11:44:56 +0100 Subject: [PATCH 2/6] Tidy up code --- .../Database/Eloquent/Casts/AsCollection.php | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/src/Illuminate/Database/Eloquent/Casts/AsCollection.php b/src/Illuminate/Database/Eloquent/Casts/AsCollection.php index e031045a533e..6d217c822dea 100644 --- a/src/Illuminate/Database/Eloquent/Casts/AsCollection.php +++ b/src/Illuminate/Database/Eloquent/Casts/AsCollection.php @@ -19,12 +19,14 @@ public static function castUsing(array $arguments) return new class implements CastsAttributes { public function get($model, $key, $value, $attributes) { - return new Collection(json_decode($attributes[$key] ?? '[]', true)); + $items = isset($attributes[$key]) ? json_decode($attributes[$key], true) : []; + + return new Collection($items); } public function set($model, $key, $value, $attributes) { - if ($this->isEmpty($value) && $this->wasEmpty($model, $key)) { + if ($this->isEmpty($value) && $this->wasNull($model, $key)) { return [$key => null]; } @@ -37,16 +39,12 @@ protected function isEmpty($value) return $value->isEmpty(); } - return ! $value; + return empty($value); } - protected function wasEmpty($model, $key) + protected function wasNull($model, $key) { - if ($model instanceof Model) { - return $model->getRawOriginal($key) === null; - } - - return false; + return $model->getRawOriginal($key) === null; } }; } From 3b796f01e21db537fa81c2b266d3927b47454557 Mon Sep 17 00:00:00 2001 From: Gerard Lamusse Date: Wed, 24 Mar 2021 11:49:28 +0100 Subject: [PATCH 3/6] Remove wasNull function --- src/Illuminate/Database/Eloquent/Casts/AsCollection.php | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/src/Illuminate/Database/Eloquent/Casts/AsCollection.php b/src/Illuminate/Database/Eloquent/Casts/AsCollection.php index 6d217c822dea..166825fc6fe8 100644 --- a/src/Illuminate/Database/Eloquent/Casts/AsCollection.php +++ b/src/Illuminate/Database/Eloquent/Casts/AsCollection.php @@ -26,7 +26,7 @@ public function get($model, $key, $value, $attributes) public function set($model, $key, $value, $attributes) { - if ($this->isEmpty($value) && $this->wasNull($model, $key)) { + if (is_null($model->getRawOriginal($key)) && $this->isEmpty($value)) { return [$key => null]; } @@ -41,11 +41,6 @@ protected function isEmpty($value) return empty($value); } - - protected function wasNull($model, $key) - { - return $model->getRawOriginal($key) === null; - } }; } } From 223cdfb0faa4ecfc04593c98922dcd917060cbf7 Mon Sep 17 00:00:00 2001 From: Gerard Lamusse Date: Wed, 24 Mar 2021 12:12:58 +0100 Subject: [PATCH 4/6] Check null instead of empty Allow setting the attribute to an empty array or even resetting back to null --- src/Illuminate/Database/Eloquent/Casts/AsCollection.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Illuminate/Database/Eloquent/Casts/AsCollection.php b/src/Illuminate/Database/Eloquent/Casts/AsCollection.php index 166825fc6fe8..d355c0fad9a0 100644 --- a/src/Illuminate/Database/Eloquent/Casts/AsCollection.php +++ b/src/Illuminate/Database/Eloquent/Casts/AsCollection.php @@ -39,7 +39,7 @@ protected function isEmpty($value) return $value->isEmpty(); } - return empty($value); + return $value === null; } }; } From 5a9c390784a90933ab7b3bae72b165af89ef967d Mon Sep 17 00:00:00 2001 From: Gerard Lamusse Date: Wed, 24 Mar 2021 15:23:47 +0100 Subject: [PATCH 5/6] Update src/Illuminate/Database/Eloquent/Casts/AsCollection.php Co-authored-by: Tom Irons --- src/Illuminate/Database/Eloquent/Casts/AsCollection.php | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/Illuminate/Database/Eloquent/Casts/AsCollection.php b/src/Illuminate/Database/Eloquent/Casts/AsCollection.php index d355c0fad9a0..01068f91309e 100644 --- a/src/Illuminate/Database/Eloquent/Casts/AsCollection.php +++ b/src/Illuminate/Database/Eloquent/Casts/AsCollection.php @@ -19,9 +19,7 @@ public static function castUsing(array $arguments) return new class implements CastsAttributes { public function get($model, $key, $value, $attributes) { - $items = isset($attributes[$key]) ? json_decode($attributes[$key], true) : []; - - return new Collection($items); + return new Collection(isset($attributes[$key]) ? json_decode($attributes[$key], true) : []); } public function set($model, $key, $value, $attributes) From 9c78665af2a15016d37faf983cb5ce8c9ff0c9c2 Mon Sep 17 00:00:00 2001 From: Gerard Lamusse Date: Wed, 24 Mar 2021 15:29:19 +0100 Subject: [PATCH 6/6] Update src/Illuminate/Database/Eloquent/Casts/AsCollection.php Co-authored-by: Tom Irons --- .../Database/Eloquent/Casts/AsCollection.php | 11 +---------- 1 file changed, 1 insertion(+), 10 deletions(-) diff --git a/src/Illuminate/Database/Eloquent/Casts/AsCollection.php b/src/Illuminate/Database/Eloquent/Casts/AsCollection.php index 01068f91309e..7e65e9998a4b 100644 --- a/src/Illuminate/Database/Eloquent/Casts/AsCollection.php +++ b/src/Illuminate/Database/Eloquent/Casts/AsCollection.php @@ -24,21 +24,12 @@ public function get($model, $key, $value, $attributes) public function set($model, $key, $value, $attributes) { - if (is_null($model->getRawOriginal($key)) && $this->isEmpty($value)) { + if (is_null($value) || ($value instanceof Collection && $value->isEmpty())) { return [$key => null]; } return [$key => json_encode($value)]; } - - protected function isEmpty($value) - { - if ($value instanceof Collection) { - return $value->isEmpty(); - } - - return $value === null; - } }; } }