Laravel的ORM有个强大的功能,可以在数据库查询或者插入的时候进行干涉。
分两种情况:
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 5.5中,取值方式加强了,可以利用如下方式,属性变成方法了,并且还可以设置默认值:
文件路径:
D:\phpStudy\WWW\BCCKidV1.0\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Concerns\HasAttributes.php
/**
* Get the model's original attribute values.
*
* @param string|null $key
* @param mixed $default
* @return mixed|array
*/
public function getOriginal($key = null, $default = null)
{
return Arr::get($this->original, $key, $default);
}
所以取值方式变为:
$user->getOriginal('gender')