Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[8.x] Add castAsJson method for database assertions #34302

Merged
merged 2 commits into from Sep 14, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -5,6 +5,7 @@
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\DB;
use Illuminate\Testing\Constraints\CountInDatabase;
use Illuminate\Testing\Constraints\HasInDatabase;
use Illuminate\Testing\Constraints\SoftDeletedInDatabase;
Expand Down Expand Up @@ -118,6 +119,19 @@ protected function isSoftDeletableModel($model)
&& in_array(SoftDeletes::class, class_uses_recursive($model));
}

/**
* Cast a JSON string to a database compatible type.
*
* @param array|string $value
* @return \Illuminate\Database\Query\Expression
*/
public function castAsJson($value)
{
$value = is_array($value) ? json_encode($value) : $value;

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

/**
* Get the database connection.
*
Expand Down