如何制作与 timestamp() 列具有相同属性的自定义列?

How to make a custom column with same properties as timestamp() columns?

我需要第三列 ( sorting_date ),其属性与 created_atupdated_at 列,它们都是用 timestamp().

生成的

我在迁移中尝试这样做:

$table->datetime('sorting_date')->default(Carbon::now());

但这给我留下了 date/time 的裸字符串,因此我无法调用其他两个存在的所有好方法,例如。 ->format('Y-m')

如何添加列 'sorting_date' 并使其成为与 created_at 相同的 class 的实例updated_at?

更新

@PuriaDeveloper 帮助我获得专栏时间:

$table->timestamp('sorting_date')->nullable()->default(Carbon::now());

但我仍然无法使用碳纤维 class 附带的便利物品。请看下面我从修补匠那里剪下来的东西:

>>>$i->sorting_date
=> "2019-01-25 11:09:57"
>>> $i->created_at
=> Illuminate\Support\Carbon @1548414597 {#3043
     date: 2019-01-25 11:09:57.0 UTC (+00:00),
   }
>>>

我需要我的专栏来生成包裹在碳汇中的日期!这是怎么做到的?

你应该使用

$table->timestamp('sorting_date');

有关更多信息,请查看此 link

您需要像这样将 'sorting_date' 添加到模型的 $dates 属性

class YourModel extends Model
{
    protected $dates = [
        'sorting_date',
   ];
}

有关更多详细信息,请查看此处的文档:

https://laravel.com/docs/5.7/eloquent-mutators#date-mutators