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 ...
随机推荐
- java代码示例(6-1)
创建Administrator.java /** * 需求分析:定义用户名,密码 * @author chenyanlong * 日期:2017/10/15 */ package com.hp.te ...
- dojo.js --dojo Quick Start/dojo入门手册1
我看了http://www.cnblogs.com/mylem/archive/2009/11/11/1600984.html这篇博客以后 ,就开始设计自己的代码,其实很多解释都是在我的代码里,所以就 ...
- Python 爬虫的工具列表 附Github代码下载链接
Python爬虫视频教程零基础小白到scrapy爬虫高手-轻松入门 https://item.taobao.com/item.htm?spm=a1z38n.10677092.0.0.482434a6E ...
- Python字符编码以及循环机制介绍
Python字符编码以及循环机制介绍 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 身为一名运维人员就得不断的学习,那么如何身为运维的你是否已经嗅探已经很火的Python编程啦?大 ...
- OS + CentOS 7 / centos 7 / VirtualBox /vegrant
s VirtualBox:解决VirtualBox安装时libSDL-1.2.so.0()错误的问题. http://www.sklinux.com/983 为Centos6.2安装VirtualBo ...
- 函数和常用模块【day04】:递归(五)
本节内容 作用域.局部和全局变量 递归 函数式编程 高阶函数和eval()函数 一.概述 在函数内部,可以调用其他函数.但是一个函数在内部调用自身,这个函数被称为递归函数. 二.简单介绍 那递归具体是 ...
- Spark记录-spark-env.sh配置
环境变量 含义 SPARK_MASTER_IP master实例绑定的IP地址,例如,绑定到一个公网IP SPARK_MASTER_PORT mater实例绑定的端口(默认7077) SPARK_MA ...
- 超哥带你学GIT
git入门 git安装 git基础 git分支 github与gitlab与git三个基佬的故事 gitlab与pycharm结合 github使用 git超清技能图 学习git站点: git官网 廖 ...
- Hibernate_day03
一.今天内容 0 列表功能实现 1 表与表之间关系回顾 (1)一对多(客户和联系人) (2)多对多(用户和角色) 2 hibernate一对多操作 (1)一对多映射配置 (2)一对多级联保存 (3)一 ...
- Django学习手册 - 初识自定义分页
核心: <a href='http://127.0.0.1:8000/index-%s'>%s<a> 自定义分页 1.前端处理字符 后端的字符 return render(r ...