Laravel中Eloquent修改器怎么实现和应用
Admin 2022-06-29 群英技术资讯 1174 次浏览
这篇文章给大家介绍了“Laravel中Eloquent修改器怎么实现和应用”的相关知识,讲解详细,步骤过程清晰,有一定的借鉴学习价值,因此分享给大家做个参考,感兴趣的朋友接下来一起跟随小编看看吧。修改器
获取
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class User extends Model {
public function getFirstNameAttribute($value) {
return ucfirst($value);
}
}
使用 Laravel 加密器 来加密一个被保存在数据库中的值,当你从 Eloquent 模型访问该属性时该值将被自动解密。
$user = App\User::find(1); $firstName = $user->first_name;
修改
public function setFirstNameAttribute ($value) {
$this->attributes['first_name'] = strtolower($value);
}
$user = App\User::find(1); $user->first_name = 'Sally';
日期转化器
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class User extends Model{
protected $dates = [
'created_at',
'updated_at',
'deleted_at'
];
}
$user = App\User::find(1); $user->deleted_at = Carbon::now(); $user->save();
可在属性上使用任何 Carbon 方法:
$user = App\User::find(1); echo $user->deleted_at->getTimestamp();
设置时间格式
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Flight extends Model {
protected $dateFormat = 'U';
}
属性类型转化
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class User extends Model {
protected $casts = [
'is_admin' => 'boolean',
];
}
现在当你访问 is_admin 属性时,它将会被转换成布尔值,即便保存在数据库里的值是一个整数:
$user = App\User::find(1);
if ($user->is_admin) {
//
}
支持的转换的类型有:
integer
real
float
double
string
boolean
object
array
collection
date
datetime
timestamp
# protected $casts = [ # 'options' => 'array', # ]; $user = App\User::find(1); $options = $user->options; $options['key'] = 'value'; $user->options = $options; $user->save();
序列化模型或集合
序列化成数组
$user = App\User::with('roles')->first();
return $user->toArray();
序列化成 JSON
$user = App\User::find(1); return $user->toJson(); // 或者 return (string) $user; // 自动调用 toJson // 或者 return App\User::all();
隐藏来自 json 的属性
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class User extends Model {
protected $hidden = ['password'];
}
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class User extends Model {
protected $visible = ['first_name', 'last_name'];
}
临时隐藏
return $user->makeVisible('attribute')->toArray();
return $user->makeHidden('attribute')->toArray();
添加参数到 json 中
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class User extends Model {
protected $appends = ['is_admin'];
}
# 在 appends 数组中的属性也遵循模型中 visible 和 hidden 设置
public function getIsAdminAttribute() {
return $this->attributes['is_admin'] == 'yes';
}
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
这篇文章带你探究一下在php中什么为什么运算符===比==要快,文章中给大家介绍的非常详细,对大家的学习或工作都具有一定的参考价值
PHP中怎样通过Red实现执行订单支付的定时任务?下文有详细的介绍,小编觉得挺实用的,对大家学习或工作或许有帮助,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。
本篇文章带大家了解一下PHP 7 和 PHP 5 中对象,并比较一下,看看它们之间的差异!
本文章向大家介绍php7连接Sqlserver的方法,主要包括php7.2连接Sqlserver使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。
今天小编就为大家分享一篇关于PHP正则验证字符串是否为数字的两种方法并附常用正则,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
成为群英会员,开启智能安全云计算之旅
立即注册关注或联系群英网络
7x24小时售前:400-678-4567
7x24小时售后:0668-2555666
24小时QQ客服
群英微信公众号
CNNIC域名投诉举报处理平台
服务电话:010-58813000
服务邮箱:service@cnnic.cn
投诉与建议:0668-2555555
Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 ICP核准(ICP备案)粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008