Laravel attribute casting 导致的 Indirect modification of overloaded property
在 Laravel model 中,设置了某个属性做 array casting.
protected $casts = [
'rounds' => 'array',
];
但是在 controller 中执行
array_push($record->rounds, date("Y-m-d H:i:s"));
时,报错
production.ERROR: Indirect modification of overloaded property
可见,casting 并不支持一些针对特定类型的操作,例如无法作为指定类型的函数的参数。
按照官方文档的做法,应该是先赋值给一个中间变量,进行操作,然后再赋值回去。
$user = App\User::find(1);
$options = $user->options;
$options['key'] = 'value';
$user->options = $options;
$user->save();
所以正确的做法应该是
$tmp = $record->rounds;
array_push($tmp, date("Y-m-d H:i:s"));
$record->rounds = $tmp;
$record->save();
collection casting
发现还有 collection casting 的支持,于是尝试了一下。
// casting 类型
- 'rounds' => 'array'
+ 'rounds' => 'collection'
// collection 的 push 操作
// 需要注意,push 之后,需要重新赋值回去。
- array_push($record->rounds, date("Y-m-d H:i:s"));
+ $record->rounds = $record->rounds->push(date("Y-m-d H:i:s"));
// 初始化
- $game_record->rounds = [];
+ $game_record->rounds = collect([]);
casting 支持的类型
integer, real, float, double, string, boolean, object, array, collection, date, datetime, and timestamp.
Laravel attribute casting 导致的 Indirect modification of overloaded property的更多相关文章
- thinkphp5中Indirect modification of overloaded element of XXX has no effect的解决办法
最近在使用Thinkphp5做foreach循环嵌套的时候报错:Indirect modification of overloaded element of XXX has no effect,网上搜 ...
- tp5框架用foreach循环时候报Indirect modification of overloaded element of think\paginator\driver\Bootst错误
thinkphp5使用paginator分页查询数据后,需要foreach便利处理某一字段的数据,会出现类似题目的错误.主要是因为tp5使用分页类读取的数据不是纯数组的格式!所以在循环的时候需要用数据 ...
- Attribute "not-null" must be declared for element type "property"解决办法
Attribute "not-null" must be declared for element type "property"解决办法 在hiberante ...
- Laravel 使用Voyager导致多个数据库连接总是返回默认连接?
问题与分析 最近的项目碰到一个奇怪的问题,在Laravel(5.3)中想建立多个数据库连接连到MySQL的不同数据库(另一个连接名为conn2),执行如下语句得到却发现得到的仍然是默认连接: $con ...
- laravel EncryptCookies中间件导致无法获取自定义cookie
解决办法: \app\Http\Middleware\EncryptCookies.php 添加过滤cookie key protected $except = [ 'token' ];
- html标签属性(attribute)和dom元素的属性(property)
简介 attribute和property都有属性之意,但对于attribute和property的区分其实并不难.从对象来说,attribute是html文档上标签属性, 而property则是对应 ...
- 从CakePHP 1.3升级到2.5
从CakePHP 1.3升级到2.5 摘要:最近把一个CakePHP 1.3的项目升级到了2.x,当然就用最新的版本2.5.3了,结果基本满意.本文记录了升级的过程,包括使用的工具,遇到的问题和相应的 ...
- 大约laravel错误的解决方案
2015-3-13 夜晚 9:13 执行laravel发生错误Indirect modification of overloaded element of BbsArticle has no effe ...
- JQuery1.11版本对prop和attr接口的含义分离导致问题分析
问题背景 实验中, 在jquery1.7版本, attr("value") 和 val() 接口获取 input 控件的值, 都是一致的, 都是当前控件值. 但是 jquery1 ...
随机推荐
- kubernetes Dashboard 使用RBAC 权限认证控制
kubernetes RBAC实战 环境准备 先用kubeadm安装好kubernetes集群,[包地址在此](https://market.aliyun.com/products/56014009/ ...
- linux_shell使用
一.hash命令 说明:linux系统下会有一个hash表,当你刚开机时这个hash表为空,每当你执行过一条命令时,hash表会记录下这条命令的路径,就相当于缓存一样.第一次执行命令shell解释器默 ...
- php-fpm sock文件权限设置
在编译php-fpm时,若没有指定fpm用户,在配置文件中也没有指定用户,则sock文件会由root(启动php-fpm的用户)创建,其权限是srw-rw----. 而nginx一般由nginx用户启 ...
- CodeForces - 163B Lemmings
B. Lemmings time limit per test 1 second memory limit per test 256 megabytes input standard input ou ...
- CodeForces - 896A Nephren gives a riddle
A. Nephren gives a riddle time limit per test 2 seconds memory limit per test 256 megabytes input st ...
- Codeforces 950 C. Zebras
http://codeforces.com/contest/950/problem/C 题意: 给出一个01序列,问能否将这个序列分为若干个0开头0结尾的序列 输出方案 如果有解,几个1能在一个序列就 ...
- 01、@ConfigurationProperties 将属性文件里的值映射到JavaBean
@ConfigurationProperties("person") //或是prefix属性 @Component //必须注册成容器中的bean被容器管理起来 public c ...
- OpenCV在字符提取中进行的预处理(转)
OCR简介熟悉OCR的人都了解,OCR大致分为两个部分: -文字提取text extractor -文字识别text recognition 其中,第一部分是属于图像处理部分,涉及到图像分割的知识,而 ...
- android get或post及HttpClient与服务器数据交互
1.Service package mydemo.mycom.demo2.service; import org.apache.http.HttpResponse; import org.apache ...
- 2017-2018-2 20155303『网络对抗技术』Exp8:Web基础
2017-2018-2 『网络对抗技术』Exp8:Web基础 --------CONTENTS-------- 一.原理与实践说明 1.实践具体要求 2.基础问题回答 二.实践过程记录 1.Web前端 ...