Skip to content

Commit

Permalink
auto handle Jsonable values passed to castAsJson() (#34392)
Browse files Browse the repository at this point in the history
`Jsonable` objects can be passed in without having to manually call `toJson()`
  • Loading branch information
browner12 authored Sep 17, 2020
1 parent 03e1373 commit 58dc491
Showing 1 changed file with 6 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)");
}
Expand Down

0 comments on commit 58dc491

Please sign in to comment.