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

Updating JSON model with sub JSON objects #16404

Closed
lukepolo opened this issue Nov 14, 2016 · 13 comments
Closed

Updating JSON model with sub JSON objects #16404

lukepolo opened this issue Nov 14, 2016 · 13 comments

Comments

@lukepolo
Copy link
Contributor

lukepolo commented Nov 14, 2016

  • Laravel Version: v5.3.22
  • PHP Version: PHP 7.0.12-1+deb.sury.org~xenial+1 (cli) ( NTS )
  • Database Driver & Version: 5.7.13

Description:

When trying to update a JSON casted column , and using sub JSON objects will produce an error

{type: "ErrorException", message: "Array to string conversion",…} file : "/home/vagrant/Code/pm/vendor/laravel/framework/src/Illuminate/Support/Str.php" line : 289 message : "Array to string conversion" type : "ErrorException"

Steps To Reproduce:

// Where `meta->tracking_numbers` are multiple JSON objects 
$program->update($request->get(['meta->tracking_numbers']));

screen shot 2016-11-14 at 10 37 24 am

@themsaid
Copy link
Member

You need to do something like:

$program->update(['meta->tracking_number' => $request->get('meta->tracking_numbers')]);

@lukepolo
Copy link
Contributor Author

Thats what Im doing currently, was hoping there would be an easier way is all

@lukepolo
Copy link
Contributor Author

Is there a specific reason why we shouldn't be allowed todo this ? If not I can look at it tonight after my work hours.

@themsaid
Copy link
Member

The update method accepts an array of columns with the new values, it's just how it was structured to work :)

@themsaid
Copy link
Member

We try to keep this repo for bug reporting only,
For suggestions and feature requests please use the https://github.com/laravel/internals repo.

@lukepolo
Copy link
Contributor Author

lukepolo commented Nov 14, 2016

@themsaid acutally

 $program->update([
            'meta->tracking_numbers' => $request->get('meta->tracking_numbers')
        ]);

Fails with the same error above, to do it I have todo

        $meta =$program->meta;
        $meta['tracking_numbers'] = $request->get('meta->tracking_numbers');

        $program->fill([
            'meta' => $meta
        ]);

        $program->save();

This is a JSON castable column , think we were talking about two different things

@themsaid
Copy link
Member

If you cast this to JSON, does it work?

$request->get('meta->tracking_numbers')

@lukepolo
Copy link
Contributor Author

No, it does not , it then gets double encoded , when retrieving from the database then it will no longer be a json object .

screen shot 2016-11-14 at 11 21 14 am

if done the "long" way, it looks like

screen shot 2016-11-14 at 11 21 41 am

@themsaid themsaid reopened this Nov 14, 2016
@themsaid
Copy link
Member

Re-opening the issue then, will take a deeper look in a few hours and get back to you :)

@lukepolo
Copy link
Contributor Author

Appreciate it~

@themsaid
Copy link
Member

The following example works as expected:

App\User::find(1)->update([
    'meta->tracking_numbers' => [(object) ['key' => 'value']]
]);

Also works if you try to save a JSON string json_encode([(object) ['key' => 'value']]).

All this with meta casted to JSON:

    protected $casts = [
        'meta' => 'json'
    ];
``

---

Please send steps to reproduce using sample data I can copy and paste into a fresh laravel application.

@lukepolo
Copy link
Contributor Author

lukepolo commented Nov 15, 2016

Another update : I had to upgrade my Laravel Version to 5.3.23 (not my MySQL Version)

#16308 fixed for future references

@ha-family
Copy link

Laravel Version: v5.3.29
PHP Version: PHP 7.0.8
Database Driver & Version: Mysql 5.7.16

i got a similar problem
when i was trying to update json column then it will occur error message as below
and i don't know how to fix it
detect_photo is JSON column and init value is {"door": {}}
and i do set detect_photo as JSON

protected $casts = [
        'detect_photo' => 'json',
];
DB::table('cars')->where('id', 11)->update(["detect_photo->door" =>  json_encode((object) ['key' => 'value']) ]);

SQLSTATE[22018]: Invalid character value for cast specification: 3156 Invalid JSON value for CAST to INTEGER from column json_extract at row 1 (SQL: update cars set detect_photo = json_set(detect_photo, "$.door", {"key":"value"}) where id = 11)

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

3 participants