访问器和修改器允许你在从模型中检索属性或设置其值时格式化属性。例如,你可能想要使用加密服务在值存储到数据库时对其进行加密,然后在模型上访问该属性时自动解密。
除了自定义访问器和修改器之外,你还可以自动将日期字段转换为 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';在这里 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 and 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 存储的列时特别有用。例如,如果您的数据库有一个包含序列化 JSON 的 TEXT 字段类型,将 array 转换添加到该属性将在您于 Eloquent 模型上访问它时自动将该属性反序列化为一个 PHP 数组:
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();