鍍金池/ 問(wèn)答/Java  PHP  數(shù)據(jù)庫(kù)  網(wǎng)絡(luò)安全/ Laravel 5.5 Eloquent JSON update 無(wú)效

Laravel 5.5 Eloquent JSON update 無(wú)效

參考資料

https://laravel-china.org/top...

問(wèn)題背景

Larvale 和MySQL 版本:

Laravel 5.5
MySQL 5.7.22

migration: ext為json 格式:

            $table->json('ext')->nullable()->comment('額外的信息');

Sample Model 我設(shè)置了casts

   protected $casts = [
        'ext' => 'object'
    ];

問(wèn)題重現(xiàn)

>>> $sample = App\Entities\Sample::find(6);
=> App\Entities\Sample {#2947
     id: 6,
     surveyor_group_id: null,
     share_code: "2sssshell2o",
     code: "222221234526",
     name: "charlie",
     mobile: "xxxxxx",
     telephone: "xxx-xxxx",
     address: "8282891",
     location_id: null,
     ext: "{"hello": "word"}",
     created_at: "2018-07-26 03:21:52",
     updated_at: "2018-07-26 03:21:52",
   }
>>> $sample->update(['ext->hello'=>'nihao']);
=> true
>>> $sample
=> App\Entities\Sample {#2947
     id: 6,
     surveyor_group_id: null,
     share_code: "2sssshell2o",
     code: "222221234526",
     name: "charlie",
     mobile: "xxxxx",
     telephone: "xxx-xxxx",
     address: "8282891",
     location_id: null,
     ext: "{"hello": "word"}",
     created_at: "2018-07-26 03:21:52",
     updated_at: "2018-07-26 03:21:52",
   }

問(wèn)題描述

>>> $sample->update(['ext->hello'=>'nihao']);

數(shù)據(jù)庫(kù)為更新沒(méi)有生效?各位大神是啥問(wèn)題呢?

回答
編輯回答
凹凸曼

已經(jīng)找到答案??雌饋?lái)是一個(gè) Laravel 的 bug

相似的問(wèn)題:

https://laracasts.com/discuss...

Eloquent 應(yīng)該寫成:

$sample->update([
    'ext'=>[
        'hello'=>'world2'
        ]
   ]);

DB class 可以寫成

$sample->update(['ext.hello'=>'world2']);
2018年5月7日 14:07