Laravel Nova:将Datetime转换为可读输出

问题描述 投票:0回答:3

Laravel Nova中是否有选项显示可读的日期时间输出和/或限制输出?

例如:2018年10月29日/ 11. 2018年11月,上午12:10

Datetime

码:

   DateTime::make('Start')
                ->rules('required')
                ->sortable(),
laravel laravel-5 laravel-nova
3个回答
2
投票

使用这个,希望它会起作用

Date::make('start')->format('F j, Y'),

3
投票

根据文件https://nova.laravel.com/docs/1.0/resources/fields.html#datetime-field

use Laravel\Nova\Fields\DateTime;

DateTime::make('Start')->format('DD MMMM YYYY'),

必须使用Moment.js格式规则https://momentjs.com/docs/#/displaying/format/


0
投票

我们也遇到了这样的问题。这个解决方案DateTime :: make('Start') - > format('DD MMMM YYYY'),仅对索引页有帮助,但对Edit页没有帮助。我不知道什么时候这个bug会在新的Nova版本中修复,但是我们暂时使用了小的硬编码。

  • 新星/ SRC /场/ Date.php
 instead: return $value->format('Y-m-d');
 use this one: return $value->format('m/d/Y');
  • 新星/资源/ JS /组件/表格/ DateField.vue
 In this vue component also should be changed a date format: dateFormat="m/d/Y".
  • 新星/资源/ JS /组件/表格/ DateField.vue
 For placeholder method use this one:
     return this.field.placeholder || moment().format('MM/DD/YYYY')
Instead this:
     return this.field.placeholder || moment().format('YYYY-MM-DD')
  • 如果以另一种格式在数据库中存储数据,也应该在App \ Model类中使用Mutator。像这样的东西:

    public function setLastUsedAttribute($value){
          $date = Carbon::createFromFormat('m/d/Y', $value);
          $this->attributes['last_used'] = $date->format('Y-m-d');
    }

© www.soinside.com 2019 - 2024. All rights reserved.