Laravel中Eloquent修改器怎么实现和应用
Admin 2022-06-29 群英技术资讯 1242 次浏览
这篇文章给大家介绍了“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怎么样快速导出百万级数据到CSV或者EXCEL文件中,因此,这篇文章就给大家分享一下php百万级数据导出的操作,下面是导出思路,导出源码以及测试,有需要的朋友可以看一下,希望大家有收获。
一个integer是集合ℤ={...,-2,-1,0,1,2,...}中的一个数。语法整型值可以使用十进制,十六进制,八进制或二进制表示,前面可以加上可选的符号(-或者+)。...
利用php的ob缓存机制实现页面静态化首先介绍一下php中ob缓存常用到的几个常用函数ob_start():开启缓存机制ob_get_contents():获取ob缓存中的内容ob_clean()清除ob缓存中的内容,但不关闭缓存ob_end_clean()清除ob缓存中的内容,并关闭缓存ob_flush清空缓存,输出内容,但不关闭缓存ob_end_flush清空缓存,输出内容,并关闭缓存
本文主要给大家分享的是laravel服务容器的内容,服务容器是用于管理类(服务)的实例化的机制,下文对laravel服务容器及使用,以及服务绑定与解析都有详细的介绍,感兴趣的朋友就继续往下看吧。
成为群英会员,开启智能安全云计算之旅
立即注册关注或联系群英网络
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