skip to Main Content

I’m trying to convert a json column name with path ‘foo->bar’ to be compatible with mysql/postgresql using Laravel’s wrapJsonSelector() method.

How can I access this method from the query builder calling getGrammar throws error:

Method IlluminateDatabaseQueryGrammarsMySqlGrammar::isJsonSelector does not exist.

Eg

$query->where(function (Builder $query) use ($searchColumn): Builder {
    if ($query->getGrammar()->isJsonSelector($searchColumn)) {
        $searchColumn = $query->getGrammar()->wrapJsonSelector($searchColumn);
    }
    // ...
})

2

Answers


  1. DB::table('your_table')
    ->selectRaw('your_json_column->"$.foo->bar" as modified_column')
    ->get();
    

    The selectRaw method allows you to write raw SQL expressions within your query.

    Login or Signup to reply.
  2. isJsonSelector and wrapJsonSelector are protected methods, which means you cannot access them from outside the class. However, you don’t need to invoke either of them directly in the first place. You can just use the wrap method, which will invoke wrapJsonSelector in the case you want to cover here.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search