获取
?
|
1
2
3
4
5
6
7
8
|
<?php
namespace App;
use Illuminate\\Database\\Eloquent\\Model;
class User extends Model {
public function getFirstNameAttribute($value) {
return ucfirst($value);
}
}
|
使用 Laravel 加密器 来加密一个被保存在数据库中的值,当你从 Eloquent 模型访问该属性时该值将被自动解密。
?
|
1
2
|
$user = App\\User::find(1);
$firstName = $user->first_name;
|
修改
?
|
1
2
3
|
public function setFirstNameAttribute ($value) {
$this->attributes['first_name'] = strtolower($value);
}
|
?
|
1
2
|
$user = App\\User::find(1);
$user->first_name = 'Sally';
|
日期转化器
?
|
1
2
3
4
5
6
7
8
9
10
|
<?php
namespace App;
use Illuminate\\Database\\Eloquent\\Model;
class User extends Model{
protected $dates = [
'created_at',
'updated_at',
'deleted_at'
];
}
|
?
|
1
2
3
|
$user = App\\User::find(1);
$user->deleted_at = Carbon::now();
$user->save();
|
可在属性上使用任何 Carbon 方法:
?
|
1
2
|
$user = App\\User::find(1);
echo $user->deleted_at->getTimestamp();
|
设置时间格式
?
|
1
2
3
4
5
6
|
<?php
namespace App;
use Illuminate\\Database\\Eloquent\\Model;
class Flight extends Model {
protected $dateFormat = 'U';
}
|
属性类型转化
?
|
1
2
3
4
5
6
7
8
|
<?php
namespace App;
use Illuminate\\Database\\Eloquent\\Model;
class User extends Model {
protected $casts = [
'is_admin' => 'boolean',
];
}
|
现在当你访问 is_admin 属性时,它将会被转换成布尔值,即便保存在数据库里的值是一个整数:
?
|
1
2
3
4
|
$user = App\\User::find(1);
if ($user->is_admin) {
//
}
|
支持的转换的类型有:
integer
real
float
double
string
boolean
object
array
collection
date
datetime
timestamp
?
|
1
2
3
4
5
6
7
8
9
|
# protected $casts = [
# 'options' => 'array',
# ];
$user = App\\User::find(1);
$options = $user->options;
$options['key'] = 'value';
$user->options = $options;
$user->save();
|
序列化模型或集合
序列化成数组
?
|
1
2
|
$user = App\\User::with('roles')->first();
return $user->toArray();
|
序列化成 JSON
?
|
1
2
3
4
5
6
|
$user = App\\User::find(1);
return $user->toJson();
// 或者
return (string) $user; // 自动调用 toJson
// 或者
return App\\User::all();
|
隐藏来自 json 的属性
?
|
1
2
3
4
5
6
7
|
<?php
namespace App;
use Illuminate\\Database\\Eloquent\\Model;
class User extends Model {
protected $hidden = ['password'];
}
|
?
|
1
2
3
4
5
6
7
|
<?php
namespace App;
use Illuminate\\Database\\Eloquent\\Model;
class User extends Model {
protected $visible = ['first_name', 'last_name'];
}
|
临时隐藏
?
|
1
2
|
return $user->makeVisible('attribute')->toArray();
return $user->makeHidden('attribute')->toArray();
|
添加参数到 json 中
?
|
1
2
3
4
5
6
7
|
<?php
namespace App;
use Illuminate\\Database\\Eloquent\\Model;
class User extends Model {
protected $appends = ['is_admin'];
}
|
?
|
1
2
3
4
|
# 在 appends 数组中的属性也遵循模型中 visible 和 hidden 设置
public function getIsAdminAttribute() {
return $this->attributes['is_admin'] == 'yes';
}
|
以上这篇laravel 之 Eloquent 模型修改器和序列化示例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持快网idc。
原文链接:https://blog.csdn.net/OneGoal/article/details/76637960
相关文章
猜你喜欢
- ASP.NET自助建站系统中如何实现多语言支持? 2025-06-10
- 64M VPS建站:如何选择最适合的网站建设平台? 2025-06-10
- ASP.NET本地开发时常见的配置错误及解决方法? 2025-06-10
- ASP.NET自助建站系统的数据库备份与恢复操作指南 2025-06-10
- 个人网站服务器域名解析设置指南:从购买到绑定全流程 2025-06-10
TA的动态
- 2025-07-10 怎样使用阿里云的安全工具进行服务器漏洞扫描和修复?
- 2025-07-10 怎样使用命令行工具优化Linux云服务器的Ping性能?
- 2025-07-10 怎样使用Xshell连接华为云服务器,实现高效远程管理?
- 2025-07-10 怎样利用云服务器D盘搭建稳定、高效的网站托管环境?
- 2025-07-10 怎样使用阿里云的安全组功能来增强服务器防火墙的安全性?
快网idc优惠网
QQ交流群
您的支持,是我们最大的动力!
热门文章
-
2025-05-27 33
-
2025-05-29 82
-
2025-05-25 17
-
2025-06-04 53
-
2025-05-29 36
热门评论

