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

Data from JSON column returned quoted when used with mysql #25726

Closed
stomaskov opened this issue Sep 21, 2018 · 8 comments
Closed

Data from JSON column returned quoted when used with mysql #25726

stomaskov opened this issue Sep 21, 2018 · 8 comments

Comments

@stomaskov
Copy link

  • 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.
@staudenmeir
Copy link
Contributor

The ->> operator does work:

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

@stomaskov
Copy link
Author

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.

@staudenmeir
Copy link
Contributor

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.

@stomaskov
Copy link
Author

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.

@staudenmeir
Copy link
Contributor

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

@stomaskov
Copy link
Author

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

Ok, thank you.

@staudenmeir
Copy link
Contributor

Will be fixed in Laravel 5.8.

@stomaskov stomaskov changed the title Data from JSON colulmn returned quoted when used with mysql Data from JSON column returned quoted when used with mysql Sep 26, 2018
@staudenmeir
Copy link
Contributor

Please close the issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants