访问器和修改器允许你在从模型中检索属性或设置其值时对其进行格式化。例如,你可能想要使用加密服务来加密一个存储在数据库中的值,然后在模型上访问该属性时自动解密它。
除了自定义访问器和修改器之外,你还可以自动将日期字段转换为 Carbon 实例或者甚至将文本值转换为 JSON。
要定义一个访问器,在你的模型上创建一个 getFooAttribute 方法,其中 Foo 是你希望访问的列的“驼峰式”命名。在本例中,我们将为 first_name 属性定义一个访问器。当尝试检索 first_name 的值时,该访问器将自动被调用:
namespace Acme\Blog\Models;
use Model;
class User extends Model
{
/**
* getFirstNameAttribute is available as `first_name` on the model
*/
public function getFirstNameAttribute($value)
{
return ucfirst($value);
}
}如你所见,列的原始值会传递给访问器,让你能够操作并返回该值。 要访问访问器的值,你可以直接访问 first_name 属性。
$user = User::find(1);
$firstName = $user->first_name;访问器可以在外部通过扩展 model.getAttribute 模型事件来定义。
User::extend(function ($model) {
$model->bindEvent('model.getAttribute', function ($attribute, $value) {
if ($attribute === 'first_name') {
return ucfirst($value);
}
});
});要定义一个修改器,请在你的模型上定义一个 setFooAttribute 方法,其中 Foo 是你希望访问的列的“驼峰式”命名。在此示例中,让我们为 first_name 属性定义一个修改器。当我们尝试设置模型上 first_name 属性的值时,此修改器将自动被调用。
namespace Acme\Blog\Models;
use Model;
class User extends Model
{
/**
* setFirstNameAttribute writes to the `first_name` attribute
*/
public function setFirstNameAttribute($value)
{
$this->attributes['first_name'] = strtolower($value);
}
}存取器将接收正要设置到属性上的值,允许你处理该值,并将处理后的值设置到模型的内部 $attributes 属性上。例如,如果我们尝试将 first_name 属性设置为 Sally。
$user = User::find(1);
$user->first_name = 'Sally';[text 保持结构](url)
被引用的text 保持结构内容将是:
setFirstNameAttribute 函数将以 Sally 值被调用。这个修改器随后将把 strtolower 函数应用于名称,并将其值设置到内部的 $attributes 数组中。
修改器可以在外部定义,通过扩展 model.setAttribute 模型事件。
User::extend(function ($model) {
$model->bindEvent('model.setAttribute', function ($attribute, $value) use ($model) {
if ($attribute === 'first_name') {
$model->attributes['first_name'] = strtolower($value);
}
});
});默认情况下, October 中的模型会将 created_at 和 updated_at 列转换为一个 Carbon 对象的实例, 它提供了各种实用的方法并扩展了原生 PHP DateTime 类.
你可以自定义哪些字段会被自动修改,甚至完全禁用此修改,方法是覆盖模型的 $dates 属性:
class User extends Model
{
/**
* @var array dates return as \Carbon\Carbon instances
*/
protected $dates = ['created_at', 'updated_at', 'disabled_at'];
}当某一列被视为日期时,您可以将其值设置为 UNIX 时间戳,日期字符串(Y-m-d),日期时间字符串,当然也可以设置为 DateTime / Carbon 实例,并且日期值将自动正确地存储到您的数据库中。
$user = User::find(1);
$user->disabled_at = Carbon::now();
$user->save();如上所述,当检索列在你的 $dates 属性中的属性时,它们将自动被转换为 Carbon 实例,允许你对你的属性使用 Carbon 的任何方法。
$user = User::find(1);
return $user->disabled_at->getTimestamp();默认情况下,时间戳的格式为 'Y-m-d H:i:s'. 如果您需要自定义时间戳格式,请设置模型上的 $dateFormat 属性. 此属性决定了日期属性在数据库中的存储方式,以及当模型序列化为数组或 JSON 时的格式:
class Flight extends Model
{
/**
* @var string dateFormat for storage of the model's date columns.
*/
protected $dateFormat = 'U';
}模型上的 $casts 属性提供了一种方便的方法,用于将属性转换为常见数据类型。$casts 属性应该是一个数组,其中键是要转换的属性的名称,而值是你希望将列转换成的类型。支持的转换类型有:integer、real、float、double、string、boolean、object 和 array。
例如,让我们将 is_admin 属性,在我们的数据库中以整数(0 或 1)的形式存储来转换为布尔值。
class User extends Model
{
/**
* @var array casts attributes to native types.
*/
protected $casts = [
'is_admin' => 'boolean',
];
}现在,is_admin 属性在您访问它时将始终被转换为布尔值,即使基础值在数据库中存储为整数。
$user = User::find(1);
if ($user->is_admin) {
//
}array 转换类型在处理存储为序列化 JSON 的列时特别有用。例如,如果您的数据库有一个 TEXT 字段类型,其中包含序列化 JSON,将 array 转换添加到该属性将自动反序列化该属性为一个 PHP 数组,当您在 Eloquent 模型上访问它时:
class User extends Model
{
/**
* @var array casts attributes to native types.
*/
protected $casts = [
'options' => 'array',
];
}定义类型转换后,你可以访问 options 属性,它将自动从 JSON 反序列化成 PHP 数组。当你设置 options 属性的值时,给定的数组将自动序列化回 JSON 以供存储:
$user = User::find(1);
$options = $user->options;
$options['key'] = 'value';
$user->options = $options;
$user->save();