Skip to content

Commit 58dc491

Browse files
authored
auto handle Jsonable values passed to castAsJson() (#34392)
`Jsonable` objects can be passed in without having to manually call `toJson()`
1 parent 03e1373 commit 58dc491

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed

src/Illuminate/Foundation/Testing/Concerns/InteractsWithDatabase.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Illuminate\Foundation\Testing\Concerns;
44

5+
use Illuminate\Contracts\Support\Jsonable;
56
use Illuminate\Database\Eloquent\Model;
67
use Illuminate\Database\Eloquent\SoftDeletes;
78
use Illuminate\Support\Arr;
@@ -127,7 +128,11 @@ protected function isSoftDeletableModel($model)
127128
*/
128129
public function castAsJson($value)
129130
{
130-
$value = is_array($value) ? json_encode($value) : $value;
131+
if ($value instanceof Jsonable) {
132+
$value = $value->toJson();
133+
} elseif (is_array($value)) {
134+
$value = json_encode($value);
135+
}
131136

132137
return DB::raw("CAST('$value' AS JSON)");
133138
}

0 commit comments

Comments
 (0)