Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
MySQL 8.0 JSON MEMBER OF Laravel Eloquent Macro
use Illuminate\Database\Query\Builder;
$memberOf = function ($value, string $column, string $path = '$', $boolean = 'and') {
return $this->whereRaw(
'? MEMBER OF(JSON_EXTRACT(%s, "%s"))',
Builder::macro('whereMemberOf', $memberOf);
Builder::macro('whereNotMemberOf', $memberOf);
Model::query()->whereMemberOf('a_json_column', 'value', '$[*]');
Model::query()->whereNotMemberOf('a_json_column', 'value', '$.field');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment