Laravel设置器修改器
2017-08-10 17:44
134 查看
碰到的问题
今天碰到个问题,我在ORM里面运用了修改器,然后Laravel查询出来的数据都变成了修改后的值,我这个结果集还需要放到下一个页面使用,并且还是需要用到修改前的值,如果在此写判断,写switch,那修改器就白瞎了,然后翻到Laravel的ORM源代码,看到修改器作用的是结果集的attributes这个数组的里面的值的,那个
original里面的数据始终都是原始结果。那就简单了,我直接运用修改前的结果就行了!!!
源代码:
在模型里面加上修改器的代码,get和Attribute之间就是数据库里面表的字段名字,$value就是查询出来的结果集里面的该字段的原始值,test是我的配置文件,里面配置了常用的错误代码,和静态数组,静态资源路径等等。大概长下面这个样子:模型里面加的修改器:
public function getResourceTypeAttribute($value) { return config('test.resource_type_arr')[$value]; }
配置文件test.php内容:
return [ //用户登录的token有效时间 'check_token_exptime'=> 2*60*60, //激活账号有效时间设置 'token_exptime' => 24 * 60 * 60, //找回密码,修改密码有效时间设置 'password_exptime' => 1 * 60 * 60, //修改邮箱,有效时间设置 'email_exptime' => 60 * 30, //WEB_URL 'web_url' => 'http://xxx.xx.0.xx8/', //LOGO #分页参数 //每页显示数量 'per_page' => 20, //默认学生数量 'learner_num' => 50, //业务编号类型 'business_no' => [ '0' => 'T000', '1' => 'X175', '2' => 'S176', '3' => 'O177', ], #============================================= //每次预约助教扣除的点数 'points' => 10, 'upload_points' => 10, //上传音频文件路径 'path_audio' => '/public/uploads/', //保存用户上传音频文件路径 'path_audio_save' => '/public/uploads/practice/', //保存用户上传头像文件路径 'path_head_image_save' => '/public/avatars/', //新注册的educator用户可能添加学生数量 'add_student_num' => 30, ];
ORM取出来的原始数据:
Resource {#390 #table: "resource" #primaryKey: "id" +timestamps: false #connection: null #perPage: 15 +incrementing: true #attributes: array:19 [ "id" => 49 "organization_id" => 2 "customer_id" => 22 "source_type" => 4 "source_type_id" => 0 "auth_code_id" => 2 "order_no" => "0" "resource_type" => 2 "auth_num" => 0 "auth_use_num" => 0 "level_id" => 1 "resource_value" => "0" "activate_time" => "2017-08-10 00:00:00" "expired_time" => "2019-08-10 00:00:00" "status" => 1 "creator" => 0 "create_time" => "2017-08-10 16:40:46" "updator" => 0 "update_time" => "2017-08-10 17:28:49" ] #original: array:19 [ "id" => 49 "organization_id" => 2 "customer_id" => 22 "source_type" => 4 "source_type_id" => 0 "auth_code_id" => 2 "order_no" => "0" "resource_type" => 2 "auth_num" => 0 "auth_use_num" => 0 "level_id" => 1 "resource_value" => "0" "activate_time" => "2017-08-10 00:00:00" "expired_time" => "2019-08-10 00:00:00" "status" => 1 "creator" => 0 "create_time" => "2017-08-10 16:40:46" "updator" => 0 "update_time" => "2017-08-10 17:28:49" ] #relations: [] #hidden: [] #visible: [] #appends: [] #fillable: [] #guarded: array:1 [ 0 => "*" ] #dates: [] #dateFormat: null #casts: [] #touches: [] #observables: [] #with: [] #morphClass: null +exists: true +wasRecentlyCreated: false }
访问方法:
dd($result['original']);
看一下结果:
array:19 [ "id" => 49 "organization_id" => 2 "customer_id" => 22 "source_type" => 4 "source_type_id" => 0 "auth_code_id" => 2 "order_no" => "0" "resource_type" => 2 "auth_num" => 0 "auth_use_num" => 0 "level_id" => 1 "resource_value" => "0" "activate_time" => "2017-08-10 00:00:00" "expired_time" => "2019-08-10 00:00:00" "status" => 1 "creator" => 0 "create_time" => "2017-08-10 16:40:46" "updator" => 0 "update_time" => "2017-08-10 17:28:49" ]
还不错,是我想要的结果!那这个问题就简单了,就这么愉快的解决了!!!顺便提一句,toArray()这个也是取的
attributes这里面的值!
相关文章推荐
- mac laravel 环境变量设置bash_profile
- Laravel 5 中使用ckfinder的问题。config.php设置。
- Laravel timestamps 设置为unix时间戳
- Laravel 5.4 全局变量设置
- laravel 之 Eloquent 模型修改器和序列化
- laravel5日志设置篇(2/3) – 记录sql日志
- laravel中ORM模型使用第二个数据库设置
- laravel资源加载路径设置
- Laravel5.2队列驱动expire参数设置带来的重复执行问题 数据库驱动
- laravel框架怎么设置公共头和公共尾
- Laravel框架路由配置总结、设置技巧大全
- laravel安装和设置流程
- Laravel5.4 不同环境下 env 文件设置
- laravel设置多个数据库连接
- nginx 各类网站设置 (laravel , thinkphp , nodejs , https)
- Laravel框架路由配置总结、设置技巧大全
- laravel 如何设置时区
- Laravel设置全局变量
- Laravel - 已登陆用户再次查看登陆页面的自动跳转设置
- 我的Laravel学习之路之 模型方法queryScope 和字段设置setAttribute