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 ...
随机推荐
- 2范数和F范数的区别
2范数和F范数是不同的. 2范数表示矩阵或向量的最大奇异值,max(svd(X)) 而 F范数表示矩阵所有元素平方和的开方根 sqrt(∑_(x_(i,j∈X))▒x_(i,j)^2 )
- 记录一个nginx的proxy_pass
server { listen 80; server_name www.hw801.com; server_name_in_redirect off; access_log /home/logs/ng ...
- Java 编程下使用 Class.forName() 加载类【转】
在一些应用中,无法事先知道使用者将加载什么类,而必须让使用者指定类名称以加载类,可以使用 Class 的静态 forName() 方法实现动态加载类.下面的范例让你可以指定类名称来获得类的相关信息. ...
- Vue加载json文件
一.在build/dev-server.js文件里 var app = express() 这句代码后面添加如下(旧版): var appData = require('../address.json ...
- UEditor百度编辑器,工具栏自定义添加一个普通按钮
根据网上前辈提供的,还真的不错,下面也整理一下 添加一个名叫“macros”的普通按钮在工具栏上: 第一步:找到ueditor.config.js文件中的toolbars数组,增加一个“macros” ...
- 阿里云服务器安装SQLServer本地无法远程访问
新买的阿里云服务器,安装上sqlserver2012,本机连接测试没有问题,但是回到本地,使用ip远程连接报错. 尝试了网上各种办法,都是失败.最后找到原因,原来在阿里云的控制台上有设置: 首先进入安 ...
- Tomcat环境变量,端口号,编码格式,项目路径,默认页的配置
Tomcat的配置 1.配置环境变量 新建名为:CATALINA_HOME的系统变量,值为我们安装tomcat的目录 2端口号及编码的配置: 找到tomcat安装目录下的sonf下的server文件, ...
- 20155209 2016-2017-2 《Java程序设计》第九周学习总结 ## 教材学习内容总结
教材学习内容总结 JDBC API 允许用户访问任何形式的表格数据,尤其是存储在关系数据库中的数据. 执行流程: •连接数据源,如:数据库. •为数据库传递查询和更新指令. •处理数据库响应并返回的结 ...
- WF控制台工作流(2)
using System; using System.Linq; using System.Activities; using System.Activities.Statements; namesp ...
- C# 简单线程实例
1.简单线程实例 以及委托(同步委托.异步委托) using System; using System.Collections.Generic; using System.Linq; using Sy ...