如何实施laravel定制碳时间戳?

Ste*_*n-v 2 php schema laravel eloquent

我希望将来的"竞赛"时间戳记在我的表格中.我可以输入没有任何问题的时间,除非我检索输入它似乎没有给我一个碳实例,但只是一个时间的字符串?

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');
}
Run Code Online (Sandbox Code Playgroud)

这就是我用来创建新竞赛的时间,表格中的timeformat与created_at和updated_at字段完全相同.当我尝试这样的事情时,他们似乎返回了一个Carbon实例:

$contest->created_at->diffForHumans()
Run Code Online (Sandbox Code Playgroud)

为什么我没有退回碳实例?

我的迁移文件如下所示:

$table->timestamps();
$table->timestamp('ends_at');
Run Code Online (Sandbox Code Playgroud)

luk*_*ter 17

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

class Contest extends Model {
    protected $dates = ['ends_at'];
}
Run Code Online (Sandbox Code Playgroud)

这告诉Laravel将ends_at属性与处理updated_at和处理相同created_at


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

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

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

  • @Jakobud不,你没有.请参阅更新的答案 (2认同)
  • 欢迎你@Stephan-v.它在[软删除部分]中提到(http://laravel.com/docs/5.0/eloquent#soft-deleting) (2认同)