Laravel 5.5 Eloquent JSON update 无效

参考资料

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

问题背景

Larvale 和MySQL 版本:

Laravel 5.5
MySQL 5.7.22

migration: ext为json 格式:
            $table->json('ext')->nullable()->comment('额外的信息');
Sample Model 我设置了casts
   protected $casts = [
        'ext' => 'object'
    ];

问题重现

>>> $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",
   }

问题描述

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

数据库为更新没有生效?各位大神是啥问题呢?

阅读 4.4k
1 个回答

已经找到答案。看起来是一个 Laravel 的 bug

相似的问题:

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

Eloquent 应该写成:

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

DB class 可以写成

$sample->update(['ext.hello'=>'world2']);
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题