您的位置:首页 > 编程语言 > PHP开发

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
这里面的值!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  orm