Friday, March 31, 2017

Change date format in datatable using Laravel and Symfony

I'm trying to change the format of a date field in the DataTable, so not in the form! These fields are created using the formbuilder Symfony. I have no clue how to do this, or were to place it. I Have tried to change the format of the date in DateForm.php but that doesn't change the data in the datatable. I hope this is enough code to understand the situation, else just tell me.

My datatable DateTable.php

class DateTable extends BaseDataTable{
    use FilterableDataTableTrait;
    public function ajax()
    {
        $view = view('admin::crud.actions');

        $query = $this->query();

        return $this->datatables
            ->eloquent($query)
            ->addColumn('id', function ($item) {
                return $item->id;
            })->addColumn('action', function ($item) use ($view) {
                $view = $item->relation ? view('admin::date.actions') : view('admin::date.actions');
                $view->with('item', $item);
                return $view->render();
            })->make(true);
    }

    public function html()
    {
        return $this->builder()
            ->columns($this->getColumns())

            ->addAction(['title' => '', 'width' => '100px'])
            ->ajax('')
            ->parameters(array_merge($this->getBuilderParameters(), ['autoWidth' => false]));
    }
 }

My form DateForm.php:

class DateForm extends AbstractType{
private $data;

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $this->data = $builder->getData();
    return $builder
        ->add('date', 'date',
            [
                'required' => true,
            ])
        ->add('description', 'textarea', ['required' => true
        ]);
}
}



via Koen van de Sande

Advertisement