From b7ceae1761de6839e37b70a65177e702b1b4a38f Mon Sep 17 00:00:00 2001 From: Tom Irons Date: Wed, 10 Mar 2021 10:18:48 -0500 Subject: [PATCH] [8.x] Allow nullable columns for AsArrayObject/AsCollection casts (#36526) * Update AsArrayObject.php * Update AsCollection.php * return null * return null * formatting --- src/Illuminate/Database/Eloquent/Casts/AsArrayObject.php | 2 +- src/Illuminate/Database/Eloquent/Casts/AsCollection.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Illuminate/Database/Eloquent/Casts/AsArrayObject.php b/src/Illuminate/Database/Eloquent/Casts/AsArrayObject.php index 8d950e2daff4..a939e8acdbdb 100644 --- a/src/Illuminate/Database/Eloquent/Casts/AsArrayObject.php +++ b/src/Illuminate/Database/Eloquent/Casts/AsArrayObject.php @@ -18,7 +18,7 @@ public static function castUsing(array $arguments) return new class implements CastsAttributes { public function get($model, $key, $value, $attributes) { - return new ArrayObject(json_decode($attributes[$key], true)); + return isset($attributes[$key]) ? new ArrayObject(json_decode($attributes[$key], true)) : null; } public function set($model, $key, $value, $attributes) diff --git a/src/Illuminate/Database/Eloquent/Casts/AsCollection.php b/src/Illuminate/Database/Eloquent/Casts/AsCollection.php index e96834f6e4d3..c2d567b504f7 100644 --- a/src/Illuminate/Database/Eloquent/Casts/AsCollection.php +++ b/src/Illuminate/Database/Eloquent/Casts/AsCollection.php @@ -19,7 +19,7 @@ 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)); + return isset($attributes[$key]) ? new Collection(json_decode($attributes[$key], true)) : null; } public function set($model, $key, $value, $attributes)