dtm37893 2015-02-16 22:30
浏览 33
已采纳

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

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?

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:

$contest->created_at->diffForHumans()

Why am I not getting a carbon instance returned?

My migration file looks like this:

$table->timestamps();
$table->timestamp('ends_at');
  • 写回答

2条回答 默认 最新

  • doujingqu3030 2015-02-16 22:44
    关注

    All you have to do is add it to the $dates property in your model.

    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


    @Jakobud You don't have to worry about overriding created_at and updated_at. They will be merged with the $dates array:

    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'

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥100 已有python代码,要求做成可执行程序,程序设计内容不多
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥100 求采集电商背景音乐的方法
  • ¥15 数学建模竞赛求指导帮助
  • ¥15 STM32控制MAX7219问题求解答
  • ¥20 在本地部署CHATRWKV时遇到了AttributeError: 'str' object has no attribute 'requires_grad'