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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 10:57:06  来源:igfitidea点击:

How to implement laravel custom carbon timestamp?

phplaravelschemaeloquent

提问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 $datesproperty in your model.

您所要做的就是将其添加到$dates模型的属性中。

class Contest extends Model {
    protected $dates = ['ends_at'];
}

This tells Laravel to treat your ends_atattribute the same as it handles updated_atand created_at

这告诉Laravel来对待你的ends_at属性相同,它处理updated_atcreated_at



@Jakobud You don't have to worry about overriding created_atand updated_at. They will be merged with the $datesarray:

@Jakobud 您不必担心覆盖created_atupdated_at. 它们将与$dates数组合并:

public function getDates()
{
    $defaults = array(static::CREATED_AT, static::UPDATED_AT);
    return array_merge($this->dates, $defaults);
}

static::CREATED_ATresolves to 'created_at'and static::UPDATED_ATto '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_atcolumn) you could define a property accessor in your Contestmodel:

Laravel 仅将其默认时间戳转换为Carbon( created_at, modified_at)。对于任何其他时间戳(例如您的ends_at列),您可以在Contest模型中定义属性访问器:

public function getEndsAtAttribute($value)
{
    return Carbon::createFromTimeStamp(strtotime($value));
}

This will convert the datetimestring returned from the database to a Carboninstance when you call $contest->ends_at.

这将在您调用时datetime将从数据库返回的字符串转换为Carbon实例$contest->ends_at