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 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程