将 Yajra 数据表的日期格式更改为服务

Change date format for Yajra Datatables as Service

我正在为我的 Laravel 项目使用 Yajra Datatables for Service,想知道我是否可以使用 Carbon 来更改日期格式。

我的数据表当前以波纹管格式显示日期

2020-11-11T16:03:13.000000Z

我想将我的日期显示为

11-11-2020 03:13 PM

我该怎么做。请帮忙。

我的数据表:

<?php

namespace App\DataTables;

// use App\App\OrderDataTable;
// use OrderDataTable;
use App\DataTables\OrderDataTable;
use Yajra\DataTables\Html\Button;
use Yajra\DataTables\Html\Column;
use Yajra\DataTables\Html\Editor\Editor;
use Yajra\DataTables\Html\Editor\Fields;
use Yajra\DataTables\Services\DataTable;
use App\Order;
use Illuminate\Support\Facades\DB;

class OrderDataTable extends DataTable
{

    public function dataTable($query){
        return datatables()
            ->eloquent($query);
    }


    public function query(OrderDataTable $model){
        $from = date('2020-11-10 00:00:00');
        $to = date('2020-11-11 23:59:59');
        // $data = Order::where('created_at', '2020-11-11 22:03:13');
        // $data = Order::select();
        $data = Order::query()
            // ->whereBetween('created_at', ['2020-11-10 00:00:00', '2020-11-11 23:59:59'])
            ->whereBetween('created_at', [$from, $to])
            ->select([
                'orders.id',
                'orders.ecomordid',
                'orders.status_id',
                'orders.awb',
                'orders.created_at'
            ]);
        return $this->applyScopes($data);
    }


    public function html(){
        return $this->builder()
                    ->setTableId('orderdatatable-table')
                    // ->columns($this->getColumns())
                    ->columns([
                        'id' => [ 'title' => 'SHIPPING CODE' ],
                        'ecomordid' => [ 'title' => 'ECOM ORDER' ],
                        'status_id' => [ 'title' => 'STATUS' ],
                        'awb' => [ 'title' => 'AWB' ],
                        'created_at' => [ 'title' => 'DATE' ],
                    ])
                    ->minifiedAjax()
                    ->dom('Bfrtip')
                    ->orderBy(0)
                    ->parameters([
                        'dom'          => 'Bfrtip',
                        'buttons'      => ['excel', 'print', 'reset', 'reload'],
                        'initComplete' => "function () {
                            this.api().columns([0,3]).every(function () {
                                var column = this;
                                var input = document.createElement(\"input\");
                                $(input).appendTo($(column.footer()).empty())
                                .on('change', function () {
                                    column.search($(this).val(), false, false, true).draw();
                                });
                            });
                        }",
                    ]);
    }


    protected function getColumns(){
        return [
            Column::make('id'),
            Column::make('ecomordid'),
            Column::make('status_id'),
            Column::make('awb'),
            Column::make('created_at'),
        ];
    }


    protected function filename(){
        return 'Order_' . date('YmdHis');
    }
}

查看:

@extends('layouts.master')

@section('content')
<meta name="csrf-token" content="{{ csrf_token() }}" />
<div class="container">
    <div class="row justify-content-center">
        <div class="col-md-12">

            <div class="card">
                <div class="card-header bg-orange"><h3>{{ __('Update By AWB') }}</h3></div>
                <div class="card-body">

                    <div class="table-responsive">
                        <div class="panel panel-default">
                            <div class="panel-heading">Sample Data</div>
                            <div class="panel-body">
                                {!! $dataTable->table([], true) !!}
                                {!! $dataTable->scripts() !!}
                            </div>
                        </div>
                    </div>

                </div>
            </div>
        </div>
    </div>
</div>
@endsection

控制器

public function order(OrderDataTable $dataTable){
        // dd($dataTable->request()->all());
        return $dataTable->render('admin.search.order');
    }

My current output

我正在使用: Laravel:7.28.4 laravel-数据表-按钮:4.0 laravel-数据表-html:4.0 laravel-数据表-甲骨文:9.14

您可以在 Order 模型中定义访问器

public function getCreatedAtAttribute($value)
{
    return Carbon::parse($value)->format('Y-m-d H:i:s');
}

请注意,无论您访问 created_at 字段,它将以这种格式获取。

阅读有关访问器的更多信息Here