Laravel 5 Eloquent cast array
属性修改 使用中的疑惑(备忘)
https://learnku.com/docs/laravel/5.5/eloqu...
Model
use Eloquent as Model;
class Series extends Model {
protected $casts = [
'field' => 'array'
];
public function getFieldAttribute($value){
return someFunction($value);
}
}
使用注意
属性修改器,提供了很方便的方式,修改Model对应的属性。但是在实际使用时,将字符串特殊处理后,转换成数组。
本来以为要 特意指定 $casts = ['field'=>'array']。但是指定后,反而不能达到预期效果。去掉以后,就正常了。
没有细究原因,写在这里是用作备忘,后续看下源代码是怎么处理的。
本作品采用《CC 协议》,转载必须注明作者和本文链接
推荐文章: