在 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的更多相关文章

  1. thinkphp5中Indirect modification of overloaded element of XXX has no effect的解决办法

    最近在使用Thinkphp5做foreach循环嵌套的时候报错:Indirect modification of overloaded element of XXX has no effect,网上搜 ...

  2. tp5框架用foreach循环时候报Indirect modification of overloaded element of think\paginator\driver\Bootst错误

    thinkphp5使用paginator分页查询数据后,需要foreach便利处理某一字段的数据,会出现类似题目的错误.主要是因为tp5使用分页类读取的数据不是纯数组的格式!所以在循环的时候需要用数据 ...

  3. Attribute "not-null" must be declared for element type "property"解决办法

    Attribute "not-null" must be declared for element type "property"解决办法 在hiberante ...

  4. Laravel 使用Voyager导致多个数据库连接总是返回默认连接?

    问题与分析 最近的项目碰到一个奇怪的问题,在Laravel(5.3)中想建立多个数据库连接连到MySQL的不同数据库(另一个连接名为conn2),执行如下语句得到却发现得到的仍然是默认连接: $con ...

  5. laravel EncryptCookies中间件导致无法获取自定义cookie

    解决办法: \app\Http\Middleware\EncryptCookies.php 添加过滤cookie key protected $except = [ 'token' ];

  6. html标签属性(attribute)和dom元素的属性(property)

    简介 attribute和property都有属性之意,但对于attribute和property的区分其实并不难.从对象来说,attribute是html文档上标签属性, 而property则是对应 ...

  7. 从CakePHP 1.3升级到2.5

    从CakePHP 1.3升级到2.5 摘要:最近把一个CakePHP 1.3的项目升级到了2.x,当然就用最新的版本2.5.3了,结果基本满意.本文记录了升级的过程,包括使用的工具,遇到的问题和相应的 ...

  8. 大约laravel错误的解决方案

    2015-3-13 夜晚 9:13 执行laravel发生错误Indirect modification of overloaded element of BbsArticle has no effe ...

  9. JQuery1.11版本对prop和attr接口的含义分离导致问题分析

    问题背景 实验中, 在jquery1.7版本, attr("value")  和 val() 接口获取 input 控件的值, 都是一致的, 都是当前控件值. 但是 jquery1 ...

随机推荐

  1. PHP依赖倒置和控制反转

    判断代码的好坏,我们有自己的标准:高内聚,低耦合.为了解决这一问题,php中有许多优秀的设计模式,比如工厂模式,单例模式. 而在代码中体现出来的设计模式,就如依赖注入和控制反转. 那什么是依赖注入? ...

  2. 泛型集合List(C#)

    using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace List ...

  3. 多目标遗传算法 ------ NSGA-II (部分源码解析)两个个体支配判断 dominance.c

    /* Domination checking routines */ # include <stdio.h> # include <stdlib.h> # include &l ...

  4. 设计模式---对象创建模式之工厂方法模式(Factory Method)

    前提:“对象创建”模式 通过“对象创建”模式绕开new,来避免对象创建(new)过程中所导致的紧耦合(依赖具体类),从而支持对象创建的稳定.它是接口抽象之后的第一步工作. 典型模式(表现最为突出) 工 ...

  5. JAVA记录-SpringMVC+Mybatis几个核心注意的地方

    1.DispatcherServlet   -- 前置控制器 DispatcherServlet是一个Servlet,所以可以配置多个DispatcherServlet. DispatcherServ ...

  6. 运用Zabbix实现内网服务器状态及局域网状况监控(5) —— Zabbix监控路由器

    1. 首先在zabbix服务器端安装snmp工具 [root@zabbix ~]# yum -y install net-snmp-utils net-snmp-libs net-snmp-devel ...

  7. SQL Server进阶(二)字段类型

    概述 系统数据类型详情 SqlDbType namespace System.Data { // // 摘要: // 指定要用于 System.Data.SqlClient.SqlParameter ...

  8. nmap学习之相关参数列表

    一.TARGET SPECIFICATION: Can pass hostnames, IP addresses, networks, etc. Ex: scanme.nmap.org, micros ...

  9. Python2和Python3中print的不同点

    在Python2和Python3中都提供print()方法来打印信息,但两个版本间的print稍微有差异 主要体现在以下几个方面: 1.python3中print是一个内置函数,有多个参数,而pyth ...

  10. HashMap、ArrayMap、SparseArray分析比较

    http://blog.csdn.net/chen_lifeng/article/details/52057427