Laravel设置器修改器

碰到的问题

今天碰到个问题,我在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这里面的值!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

SHUIPING_YANG

你的鼓励是我创作的最大动力。

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值