0

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 Illuminate\Database\Query\Grammars\MySqlGrammar::isJsonSelector does not exist.

Eg

$query->where(function (Builder $query) use ($searchColumn): Builder {
    if ($query->getGrammar()->isJsonSelector($searchColumn)) {
        $searchColumn = $query->getGrammar()->wrapJsonSelector($searchColumn);
    }
    // ...
})
2
  • 1
    "How can I access this method" It's protected, so you can't. Commented Oct 13, 2023 at 20:09
  • Gosh I hadn't realised that usually get a different error. Is there a way of opening it up via a macro? Commented Oct 13, 2023 at 21:11

3 Answers 3

1
+300

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.

Sign up to request clarification or add additional context in comments.

Comments

1

The wrapJsonSelector method is not accessible (protected), but it is called in the wrap method.

$expression = $query->getGrammar()->wrap("table.column->?"); // "table"."column"->>'?'
$query->whereRaw($expression, ['value']);

Comments

0
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.

1 Comment

Not looking to do this manually due to needing to support MySQL and Postgresql reason for wanting to access wrapJsonSelector where Laravel handles json_unquote & json_extract

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.