Laravel中Eloquent修改器怎么实现和应用
Admin 2022-06-29 群英技术资讯 1152 次浏览
这篇文章给大家介绍了“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下Ajax跨域如何解决?很多PHP新手都会遇到Ajax跨域问题,但是不知道要如何解决,对此,这篇文章就给就大家分享三种方法。感兴趣的朋友就继续往下看吧。
对于php发送网络请求,我们最常用的请求就是curl,有时我们也会用到file_get_contents函数发送网络请求。本文为大家总结了php实现网络请求的方法及函数,感兴趣的可以学习一下
今天小编就为大家整理了一篇Thinkphp 在api开发中异常返回依然是html的解决方式,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
PHP怎么实现读取excel导入数据库?这篇文文章主要介绍基于ThinkPHP 框架实现的读取excel导入数据库操作,下面分享了具体的实现代码,有需要的朋友可以参考一下。
在之前的文章中给大家带来了《让人心动简单易记的PHP变量知识小结(详细)》,本篇继续给大家带来了PHP知识的讲解,介绍一下PHP中常见的if else语法和NULL数据类型,都是基础的知识整理,希望能帮助到大家!
成为群英会员,开启智能安全云计算之旅
立即注册关注或联系群英网络
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