From b5a3795ab087062fb403c8f6600b440367f4115e Mon Sep 17 00:00:00 2001 From: Andrew Brown Date: Thu, 17 Sep 2020 11:38:43 -0500 Subject: [PATCH] auto handle `Jsonable` values passed to `castAsJson()` `Jsonable` objects can be passed in without having to manually call `toJson()` --- .../Foundation/Testing/Concerns/InteractsWithDatabase.php | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/Illuminate/Foundation/Testing/Concerns/InteractsWithDatabase.php b/src/Illuminate/Foundation/Testing/Concerns/InteractsWithDatabase.php index 012952155777..feb3d01dc591 100644 --- a/src/Illuminate/Foundation/Testing/Concerns/InteractsWithDatabase.php +++ b/src/Illuminate/Foundation/Testing/Concerns/InteractsWithDatabase.php @@ -2,6 +2,7 @@ namespace Illuminate\Foundation\Testing\Concerns; +use Illuminate\Contracts\Support\Jsonable; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\SoftDeletes; use Illuminate\Support\Arr; @@ -127,7 +128,11 @@ protected function isSoftDeletableModel($model) */ public function castAsJson($value) { - $value = is_array($value) ? json_encode($value) : $value; + if ($value instanceof Jsonable) { + $value = $value->toJson(); + } elseif (is_array($value)) { + $value = json_encode($value); + } return DB::raw("CAST('$value' AS JSON)"); }