Skip to content

mysql json field cannot update nested array item through nested array's index #26415

Closed
@eleven26

Description

@eleven26
  • Laravel Version: 5.7.12
  • PHP Version: 7.1.14
  • Database Driver & Version: MySQL 5.7.17

Description:

Laravel framework cannot update array item through array index like: '$.test[0].name', the sql generate by \Illuminate\Database\Query\Grammars\Grammar::wrapJsonPath seem like this '$."test[0]".name', but with double quotation mark won't update MySQL json field, at the same time, the update statement return the wrong result without updating MySQL's record, database record not updated but get the true result.

source code

    /**
     * Wrap the given JSON path.
     *
     * @param  string  $value
     * @return string
     */
    protected function wrapJsonPath($value)
    {
        return '\'$."'.str_replace('->', '"."', $value).'"\'';
    }

Steps To Reproduce:

  • MySQL test
    • set @json = '{"test": [{"name": "laravel"}, {"name": "symfony"}]}';
    • run select json_set(@json, '$.test[0].name', "lumen");, no double quotation mark
      • result: {"test": [{"name": "lumen"}, {"name": "symfony"}]} , update successfully
    • run select json_set(@json, '$."test[0]".name', "lumen");, with double quotation mark
      • result: {"test": [{"name": "laravel"}, {"name": "symfony"}]} , update failed
  • Laravel test
    • The sql generate by MySQL grammer:
      update `xx` set `a` = json_set(`a`, '$."test[0]"."name"', lumen)
  • This sql has double quotes wrap up test[0], but it will not work for array in MySQL json field. Unless remove the double quotes.

Related commit #22118

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions