laravel 如何实现laravel自定义碳时间戳?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28551538/
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
How to implement laravel custom carbon timestamp?
提问by Stephan-v
I want to have a future timestamp for 'contests' that expire in my table. I can input the time without any problem except when I retrieve the input it doesn't seem to return me a carbon instance but merely a string with the time?
我希望在我的表中过期的“比赛”有一个未来的时间戳。我可以毫无问题地输入时间,除非我检索输入时它似乎没有返回一个 carbon 实例,而只是一个带有时间的字符串?
public function store(ContestRequest $request)
{
$input = Request::all();
// Set the 'owner' attribute to the name of the currently logged in user(obviously requires login)
$input['owner'] = Auth::user()->name;
// Set the enddate according to the weeks that the user selected with the input select
$weeks = Request::input('ends_at');
// Add the weeks to the current time to set the future expiration date
$input['ends_at'] = Carbon::now()->addWeeks($weeks);
Contest::create($input);
return redirect('contests');
}
This is what I am using to create a new contest, the timeformat is exactly the same in the table as the created_at and updated_at fields. They seem to return a Carbon instance when I try something like:
这是我用来创建新比赛的内容,表中的时间格式与 created_at 和 updated_at 字段完全相同。当我尝试以下操作时,它们似乎返回一个 Carbon 实例:
$contest->created_at->diffForHumans()
Why am I not getting a carbon instance returned?
为什么我没有得到返回的 carbon 实例?
My migration file looks like this:
我的迁移文件如下所示:
$table->timestamps();
$table->timestamp('ends_at');
回答by lukasgeiter
All you have to do is add it to the $dates
property in your model.
您所要做的就是将其添加到$dates
模型的属性中。
class Contest extends Model {
protected $dates = ['ends_at'];
}
This tells Laravel to treat your ends_at
attribute the same as it handles updated_at
and created_at
这告诉Laravel来对待你的ends_at
属性相同,它处理updated_at
和created_at
@Jakobud You don't have to worry about overriding created_at
and updated_at
. They will be merged with the $dates
array:
@Jakobud 您不必担心覆盖created_at
和updated_at
. 它们将与$dates
数组合并:
public function getDates()
{
$defaults = array(static::CREATED_AT, static::UPDATED_AT);
return array_merge($this->dates, $defaults);
}
static::CREATED_AT
resolves to 'created_at'
and static::UPDATED_AT
to 'updated_at'
static::CREATED_AT
解析为'created_at'
与static::UPDATED_AT
以'updated_at'
回答by Bogdan
Laravel only converts it's default timestamps to Carbon(created_at
, modified_at
). For any other timestamps (such as your ends_at
column) you could define a property accessor in your Contest
model:
Laravel 仅将其默认时间戳转换为Carbon( created_at
, modified_at
)。对于任何其他时间戳(例如您的ends_at
列),您可以在Contest
模型中定义属性访问器:
public function getEndsAtAttribute($value)
{
return Carbon::createFromTimeStamp(strtotime($value));
}
This will convert the datetime
string returned from the database to a Carbon
instance when you call $contest->ends_at
.
这将在您调用时datetime
将从数据库返回的字符串转换为Carbon
实例$contest->ends_at
。