Laravel修改器取出原始值
2017-10-25 12:03
232 查看
Laravel的ORM有个强大的功能,可以在数据库查询或者插入的时候进行干涉。
分两种情况:
现在的需求是当我从数据库取出pay_type的时候,显示人能辨识的字符串而不是不知所云的数字编码,那就用如下代码即可实现。
#table: “order”
#primaryKey: “id”
+timestamps: false
#connection: null
#perPage: 15
+incrementing: true
#attributes: array:30 [▼
“id” => 194
“order_no” => “X17547041338090518”
“order_type” => 1
“order_source” => 1
“customer_type” => 1
“apply_name” => “wer werw”
“apply_email” => “zxcz35288@163.com”
“pay_type” => 3
“customer_id” => 182
“customer_name” => “wer werw”
“customer_phone” => “+0093 4234”
“customer_email” => “zxcz35288@163.com”
“address_id” => 10
“should_payment” => “29.99”
“actual_payment” => “29.99”
“other_payment” => “0.00”
“total_item_price” => “0.00”
“allow_use_coupon” => 1
“coupon_type” => 0
“coupon_id” => 0
“coupon_payment” => “0.00”
“is_online” => 1
“is_user_delete” => 0
“invoice_apply_status” => 0
“status” => 5
“remark” => null
“creator” => 1
“create_time” => “2017-09-04 17:59:06”
“updator” => null
“update_time” => “2017-09-04 18:02:42”
]
#original: array:30 [▼
“id” => 194
“order_no” => “X17547041338090518”
“order_type” => 1
“order_source” => 1
“customer_type” => 1
“apply_name” => “wer werw”
“apply_email” => “zxcz35288@163.com”
“pay_type” => 3
“customer_id” => 182
“customer_name” => “wer werw”
“customer_phone” => “+0093 4234”
“customer_email” => “zxcz35288@163.com”
“address_id” => 10
“should_payment” => “29.99”
“actual_payment” => “29.99”
“other_payment” => “0.00”
“total_item_price” => “0.00”
“allow_use_coupon” => 1
“coupon_type” => 0
“coupon_id” => 0
“coupon_payment” => “0.00”
“is_online” => 1
“is_user_delete” => 0
“invoice_apply_status” => 0
“status” => 5
“remark” => null
“creator” => 1
“create_time” => “2017-09-04 17:59:06”
“updator” => null
“update_time” => “2017-09-04 18:02:42”
]
#relations: []
#hidden: []
#visible: []
#appends: []
#fillable: []
#guarded: array:1 [▼
0 => “*”
]
#dates: []
#dateFormat: null
#casts: []
#touches: []
#observables: []
#with: []
#morphClass: null
+exists: true
+wasRecentlyCreated: false
}
在我们toArray()转换为数组之前,是这样的,仔细看,里面是不是有个origin字段,其实这个就是修改器起作用之前的数据,取出来也很简单:
我们toArray()的时候,实际作用的是attributes,是将这个转换为实际显示出来的数组的.这个功能非常棒,肯定会派上用场,有奇效!
分两种情况:
1、修改器:
修改器顾名思义就是从数据库取出来的时候修改某个字段的实际值,例如我们存在数据库一般都用代号来存储某些字符串,如下:pay_type=1代表汇丰银行支付。。。。现在的需求是当我从数据库取出pay_type的时候,显示人能辨识的字符串而不是不知所云的数字编码,那就用如下代码即可实现。
public function getPayTypeAttribute($value) { $paytype = [ 0 => '未支付', 1 => '汇丰银行支付', 2 => '支付宝支付', 3 => '银联支付', 4 => '余额支付', 5 => 'POS机支付', 6 => '现金支付', 7 => '合约支付', 8 => '免费', ]; return $paytype[$value]; }
2、设置器:
明白了修改器,再来理解设置器就很简单了,就是把接收到的字符串数据或者其他类型的东东,在存入数据库之前进行设置成我们在数据库设计的代码或者编号。public function setPayTypeAttribute($value) { }
3、这里需要注意的是,我们使用了修改器,但是某些时候,我们需要用到数据库里面真实存储的编码,例如查询的时候,我们想要查询的是支付类型为余额支付,那么,我们就需要查询pay_type=4的类型的订单,这里就有个技巧,并不是说使用了修改器,起源真实的值我们就取不到了,例如:我们打印一下获取到的某条订单的详细记录:
Order {#212 ▼#table: “order”
#primaryKey: “id”
+timestamps: false
#connection: null
#perPage: 15
+incrementing: true
#attributes: array:30 [▼
“id” => 194
“order_no” => “X17547041338090518”
“order_type” => 1
“order_source” => 1
“customer_type” => 1
“apply_name” => “wer werw”
“apply_email” => “zxcz35288@163.com”
“pay_type” => 3
“customer_id” => 182
“customer_name” => “wer werw”
“customer_phone” => “+0093 4234”
“customer_email” => “zxcz35288@163.com”
“address_id” => 10
“should_payment” => “29.99”
“actual_payment” => “29.99”
“other_payment” => “0.00”
“total_item_price” => “0.00”
“allow_use_coupon” => 1
“coupon_type” => 0
“coupon_id” => 0
“coupon_payment” => “0.00”
“is_online” => 1
“is_user_delete” => 0
“invoice_apply_status” => 0
“status” => 5
“remark” => null
“creator” => 1
“create_time” => “2017-09-04 17:59:06”
“updator” => null
“update_time” => “2017-09-04 18:02:42”
]
#original: array:30 [▼
“id” => 194
“order_no” => “X17547041338090518”
“order_type” => 1
“order_source” => 1
“customer_type” => 1
“apply_name” => “wer werw”
“apply_email” => “zxcz35288@163.com”
“pay_type” => 3
“customer_id” => 182
“customer_name” => “wer werw”
“customer_phone” => “+0093 4234”
“customer_email” => “zxcz35288@163.com”
“address_id” => 10
“should_payment” => “29.99”
“actual_payment” => “29.99”
“other_payment” => “0.00”
“total_item_price” => “0.00”
“allow_use_coupon” => 1
“coupon_type” => 0
“coupon_id” => 0
“coupon_payment” => “0.00”
“is_online” => 1
“is_user_delete” => 0
“invoice_apply_status” => 0
“status” => 5
“remark” => null
“creator” => 1
“create_time” => “2017-09-04 17:59:06”
“updator” => null
“update_time” => “2017-09-04 18:02:42”
]
#relations: []
#hidden: []
#visible: []
#appends: []
#fillable: []
#guarded: array:1 [▼
0 => “*”
]
#dates: []
#dateFormat: null
#casts: []
#touches: []
#observables: []
#with: []
#morphClass: null
+exists: true
+wasRecentlyCreated: false
}
在我们toArray()转换为数组之前,是这样的,仔细看,里面是不是有个origin字段,其实这个就是修改器起作用之前的数据,取出来也很简单:
$data=$result['original]
我们toArray()的时候,实际作用的是attributes,是将这个转换为实际显示出来的数组的.这个功能非常棒,肯定会派上用场,有奇效!
相关文章推荐
- laravel 之 Eloquent 模型修改器和序列化
- Laravel设置器修改器
- Laravel记录一之修改器和访问器
- laravel的修改器和访问器
- 关于laravel5.2从数据库取出的时间戳与本地时间不相同
- Laravel 关系模型取出需要的字段
- how to do a mass update in Laravel5 ( 在Laravel 5里面怎么做大量数据更新 )
- 5.关于laravel框架中Contorller
- laravel 路由无法跟踪的问题
- Laravel控制器和视图
- 将字符串符合规则的字符取出
- Laravel是怎么实现autoload的?
- 如何在 Laravel 5.1 中使用 Laravel Elixir 集成安装 Bootstrap
- 安装 Laravel 遇到问题?你需要更新 composer.json 文件
- laravel redis
- Laravel 5.4 整合SendCloud邮件服务
- Laravel 校验规则之字段值唯一性校验
- laravel前端内容编译:小白的惨痛经历
- 取出json解析出来的对象列表的元素时报错java.lang.ClassCastException: com.google.gson.internal.LinkedTreeMap cannot be
- 小白学习laravel/lumen(一)前言