Framework: Data from JSON column returned quoted when used with mysql

Created on 21 Sep 2018  路  8Comments  路  Source: laravel/framework

  • Laravel Version: 5.7.5
  • PHP Version: 7.2.9
  • Database Driver & Version: MySQL v5.7.23, Postgres v10.5

Description:

When selecting data from a JSON column and using MySQL data is returned quoted.
If database driver is changed to Postgres then it's ok.
Mysql docs suggests using ->> operator but that doesn't work with eloquent.

Steps To Reproduce:

  1. Have a table with JSON column and nested data.
  2. Select data using something like
    MyModel::where( 'level0->level1->myString', 'like', '%'.strtoupper($searchTerm).'%' ) ->select([ 'level0->level1->string1 as name', ]) ->get() ->toArray();
  3. If using MySql "name" value will be returned quoted.

Most helpful comment

Will be fixed in Laravel 5.8.

All 8 comments

The ->> operator does work:

->select(['level0->>level1->>string1 as name'])

The ->> operator does work:

->select(['level0->>level1->>string1 as name'])

Yes, this works for MySql.
But if I then switch to Postgres it doesn't return any result since "->>" on first element returns text instead of object.

You want to use the same code with different databases? You'll have to use something like if(DB::connection()->getName() == 'mysql') { } to execute different queries.

You want to use the same code with different databases? You'll have to use something like if(DB::connection()->getName() == 'mysql') { } to execute different queries.

I thought about it but isn't the point of using orm so I don't have to?
It propertly transforms 'column->level0->property_name as name to postgres syntax - 'column->level0->>property_name as name and also partially to mysql, which is column`->'$."level0"."property_name"' as `name.
I needs to change -> to ->> for mysql version.

The MySQL implementation behaves differently for historical reasons. I agree that this is not ideal. I'll look into a PR.

The MySQL implementation behaves differently for historical reasons. I agree that this is not ideal. I'll look into a PR.

Ok, thank you.

Will be fixed in Laravel 5.8.

Please close the issue.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

JamborJan picture JamborJan  路  3Comments

gabriellimo picture gabriellimo  路  3Comments

RomainSauvaire picture RomainSauvaire  路  3Comments

Fuzzyma picture Fuzzyma  路  3Comments

YannPl picture YannPl  路  3Comments