gpt4 book ai didi

php - 如何实现 laravel 自定义碳时间戳?

转载 作者:行者123 更新时间:2023-12-02 05:23:54 25 4
gpt4 key购买 nike

我想要在我的表中过期的“比赛”的 future 时间戳。我可以毫无问题地输入时间,除了当我检索输入时它似乎没有返回一个 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');
}

这就是我用来创建新比赛的内容,表中的时间格式与 created_at 和 updated_at 字段完全相同。当我尝试类似的东西时,他们似乎返回了一个 Carbon 实例:

$contest->created_at->diffForHumans()

为什么我没有返回 carbon 实例?

我的迁移文件如下所示:

$table->timestamps();
$table->timestamp('ends_at');

最佳答案

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

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

这告诉 Laravel 像对待 updated_atcreated_at 一样对待你的 ends_at 属性


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

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

static::CREATED_AT 解析为 'created_at'static::UPDATED_AT 解析为 'updated_at'

关于php - 如何实现 laravel 自定义碳时间戳?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28551538/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com