Laravel:如何在模型属性转换上设置日期格式?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37857479/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel: how to set date format on model attribute casting?
提问by fico7489
I have in model:
我有模型:
protected $casts = [
'date' => 'date',
];
Does laravel have some ability to set cast format, something like:
laravel 是否具有设置强制转换格式的能力,例如:
protected $casts = [
'date' => 'date_format:d/m/yyyy',
];
?
?
EDITED
已编辑
I tried this:
我试过这个:
In model:
在模型中:
protected $dateFormat = 'm/d/Y';
protected $dates = ['driver_expiration', 'created_at', 'updated_at', 'deleted_at'];
protected $casts = [
'driver_expiration' => 'date',
];
I saved date(driver_expiration) as '01/012016' but date is saved 0000-00-00.
我将日期(driver_expiration)保存为“01/012016”,但日期保存为 0000-00-00。
laravel documentation: https://laravel.com/docs/5.1/eloquent-mutatorsTell us $dateFormat works only for timestamps ('created_at', 'updated_at', 'deleted_at')
laravel 文档:https://laravel.com/docs/5.1/eloquent-mutators 告诉我们 $dateFormat 仅适用于时间戳('created_at'、'updated_at'、'deleted_at')
回答by Hemerson Varela
After laravel 5.6,
在 Laravel 5.6 之后,
You can individually customize the format of Eloquent date and datetime casting:
您可以单独自定义 Eloquent 日期和日期时间转换的格式:
protected $casts = [
'birthday' => 'date:Y-m-d',
'joined_at' => 'datetime:Y-m-d H:00',
];
This format is used when the model is serialized to an array or JSON data
当模型序列化为数组或JSON数据时使用此格式
回答by Niraj Shah
Instead of casting a date format, you can use mutators to set the format you like:
您可以使用 mutators 来设置您喜欢的格式,而不是强制转换日期格式:
For example, if your column was called date
, you can add the following function to your model:
例如,如果您的列名为date
,您可以将以下函数添加到您的模型中:
public function setDateAttribute( $value ) {
$this->attributes['date'] = (new Carbon($value))->format('d/m/y');
}
The setDateAttribute
is always in the format set[ColumnName]Attribute
, using CamelCase. The date
in the $this->attributes['date']
part must also match your actual column name in the table.
在setDateAttribute
总是在格式set[ColumnName]Attribute
,使用驼峰。将date
在$this->attributes['date']
部分也必须在该表符合您实际的列名。
When you create
or update
a record in your database, the format will automatically be changed by the function above.
当您create
或update
您的数据库中的一条记录时,格式将被上述功能自动更改。
Note: You may need to add the use Carbon\Carbon
statement to the top of your Model to use the Carbon library.
注意:您可能需要将use Carbon\Carbon
语句添加到模型的顶部以使用 Carbon 库。
Take a look at other examples here.
UPDATE
更新
Optionally, you can define a format for date
columns using:
或者,您可以date
使用以下方法定义列的格式:
protected $dateFormat = 'Y-m-d';
回答by M Muller
Just add to the model:
只需添加到模型中:
protected function asDateTime($value)
{
return parent::asDateTime($value)->format('d/m/y');
}
this Eloquent's method originally return a Carbon instance, so you can get that instance and calls to its format() method. If you wish to apply this solution to all models, just create a new class extended from Eloquent, and put this method there, so then you can extend the models from this new class.
这个 Eloquent 的方法最初返回一个 Carbon 实例,因此您可以获取该实例并调用它的 format() 方法。如果您希望将此解决方案应用于所有模型,只需创建一个从 Eloquent 扩展的新类,并将此方法放在那里,然后您就可以从这个新类扩展模型。
回答by fico7489
I create new model property:
我创建新的模型属性:
protected $datesConvert = ['driver_expiration'];
And then I updated my base model like :
然后我更新了我的基本模型,如:
public function save(array $options = [])
{
if(isset($this->datesConvert)){
foreach($this->datesConvert as $date){
$this->attributes[$date] = \Carbon\Carbon::createFromFormat('d/m/Y', $this->attributes[$date])->format('Y-m-d');
}
}
parent::save($options);
}
public function getAttribute($key)
{
$value = parent::getAttribute($key);
if(isset($this->attributes[$key])){
if(isset($this->datesConvert) && in_array($key, $this->datesConvert)){
$value = \Carbon\Carbon::createFromFormat('Y-m-d', $value)->format('d/m/Y');
}
}
return $value;
}
and this solution works for me. Code can be moved to trait. Format can be put in $datesConvert Format = 'd/m/Y';
这个解决方案对我有用。代码可以移动到 trait。格式可以放在 $datesConvert Format = 'd/m/Y';
回答by Turan Zamanl?
Casts are not affected by the Collection object. It's used for json and array formats. If you need an individual cast that you can use
转换不受 Collection 对象的影响。它用于 json 和数组格式。如果您需要可以使用的个人演员表
$user->date->format('m-d-Y');