最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - Yajra Datatables changes datetimes incorrectly - Stack Overflow

programmeradmin1浏览0评论

I am working on Laravel 11 app. I am using yajra/datatables and datetimes in the db are correct but however when yajra/datatables displays them in the frontend they are an hour behind? I've been googling for an hour can't find anything useful. It happens for all my datatables. My timezone in app.php is "Europe/Berlin". Here is an example DataTable file:

<?php

namespace App\DataTables;

use App\Helpers\Helper;
use App\Models\Tool;
use Illuminate\Database\Eloquent\Builder as QueryBuilder;
use Yajra\DataTables\EloquentDataTable;
use Yajra\DataTables\Html\Builder as HtmlBuilder;
use Yajra\DataTables\Html\Column;
use Yajra\DataTables\Services\DataTable;
 
class PeopleDataTable extends DataTable
{
    public function dataTable(QueryBuilder $query): EloquentDataTable
    {
        return (new EloquentDataTable($query))->setRowId('id');
    }
 
    public function query(Tool $model): QueryBuilder
    {
        return $model->newQuery();
    }
 
    public function html(): HtmlBuilder
    {
        return $this->builder()
                    ->setTableId('people-table')
                    ->parameters(Helper::datatablesLanguageParameters())
                    ->columns($this->getColumns())
                    ->minifiedAjax()
                    ->selectStyleSingle()
                    ->orderBy(0, 'asc')
                    ->ajax(route('people.data'));
    }
 
    public function getColumns(): array
    {
        return [
            Column::make('id'),
            ['name' => 'name', 'data' => 'name', 'title' => __('text.name')],
            ['name' => 'department', 'data' => 'department', 'title' => __('text.department')],
            ['name' => 'phone', 'data' => 'phone', 'title' => __('text.phone')],
            ['name' => 'email', 'data' => 'email', 'title' => __('text.email')],
            ['name' => 'updated_at', 'data' => 'updated_at', 'title' => __('text.updated_at')],
            ['name' => 'actions', 'data' => 'actions', 'title' => __('text.actions')],
        ];
    }
 
    protected function filename(): string
    {
        return 'People_'.date('YmdHis');
    }
}

and here is the data route in the controller:

/**
 * Provides data in json for the indexTableView endpoint.
 * 
 * @return JsonResponse 
 * @throws Exception 
 */
function data()
{
    $people = Person::query()
        ->select(
            [
                'people.id', 
                'people.name', 
                'people.department',
                'people.phone',
                'people.email',
                'people.created_at', 
                'people.updated_at', 
                'rfids.rfid_unique_id',
                'rfids.status',
            ]
        )
        ->leftJoin('rfids', 'rfids.id', '=', 'people.rfid_id');

    return (new DataTables)
        ->eloquent($people)
        ->addColumn('status', function (Person $person) {
            return HtmlService::statusHtml($person);
        })
        ->addColumn('actions', function (Person $person) {
            return HtmlService::generateActions($person, 'person');
        })
        ->rawColumns(['status', 'actions'])
        ->toJson();
}

EDIT 1! When I add this:

->editColumn('updated_at', function (Person $person) {
            return $person->updated_at;
        })

the datetime is correct. Still, I don't want to add editColumn to all my datatables there must be a way to disable this behaviour.

发布评论

评论列表(0)

  1. 暂无评论