Pirex360
Pirex360

Reputation: 386

Yajra Datatables - Column Date Format

laravel 8 with yajra Datatables. The datatable is created well, but i need to custom some column data.

return [
            Column::computed('action')
                ->exportable(false)
                ->printable(false)
                ->width(60)
                ->addClass('text-center'),
            Column::make('id')
                ->title('ID'),
            Column::make('name')
                ->title('Nome Acabamento'),
            Column::make('enabled')
                ->title('Visível'),
            Column::make('created_at'),
            Column::make('updated_at'),
        ];

In this example i need the created_at to be date format (d-m-Y) and id to be a sum operation from id + 1000.

Upvotes: 0

Views: 6796

Answers (4)

NOURDDINE BENYAHYA
NOURDDINE BENYAHYA

Reputation: 35

if you are using mysql and laravel 10 ,you can change format by mysql request

for example im working with document table and created_at column :

        ->addColumn('created_at_formatted', function ($document) {
            // Format the created_at column using MySQL date_format
            return $document->created_at->format('Y-m-d H:i:s'); 
            // Change the format as needed
        })

then add the column into your datatable

protected function getColumns()
{
    return [
        ['data' => 'created_at_formatted', 'name' => 'created_at_formatted', 'title' => 'Date'], // Add this line
    ];
}

Upvotes: 0

Marvel C
Marvel C

Reputation: 1

For example case : 

->editColumn('end_date', function($model){
            $formatDate = date('d-m-Y',strtotime($model->end_date));
            return $formatDate;
        })
->addColumn('legal','{{$model->legals->name}}')
->rawColumns(['action'])
->toJson();

Upvotes: 0

nawt12
nawt12

Reputation: 11

In Laravel 10, we use the following code for dataTables10.

 ->editColumn('created_at', function (Outcome $outcome) {
            return \Carbon\Carbon::parse($outcome->created_at )->isoFormat('DD.MM.YYYY');
        })

Upvotes: 1

Pirex360
Pirex360

Reputation: 386

I got it :

->editColumn('created_at', function($data){ $formatedDate = Carbon::createFromFormat('Y-m-d H:i:s', $data->created_at)->format('d-m-Y'); return $formatedDate; })

Upvotes: 6

Related Questions