yajra / laravel-datatables

jQuery DataTables API for Laravel
https://yajrabox.com/docs/laravel-datatables
MIT License
4.76k stars 858 forks source link

Internal Server Error: 500 in Laravel 5.5 using Ajax in DataTables #1407

Closed aescasio closed 7 years ago

aescasio commented 7 years ago

Hi,

Before in Laravel 5.4 the datables render with no problem, while using Laravel 5.5 it generate Internal Server Error: 500.

Popup

localhost:8000 says:

DataTables warning: table id=dataTableBuilder - Ajax error. For more information about this error, please >see http://datatables.net/tn/7

I use this code below in Laravel 5.5.11 which is exactly the same with the previous version Laravel 5.4:

namespace App\DataTables;

use App\Models\Policy;
use Form;
use Yajra\DataTables\Services\DataTable;

class PolicyDataTable extends DataTable
{

    /**
     * @return \Illuminate\Http\JsonResponse
     */
    public function ajax()
    {
        return $this->datatables
            ->eloquent($this->query())
            ->addColumn('action', 'policies.datatables_actions')
            ->make(true);
    }

    /**
     * Get the query object to be processed by datatables.
     *
     * @return \Illuminate\Database\Query\Builder|\Illuminate\Database\Eloquent\Builder
     */
    public function query()
    {
        $policies = Policy::query();

        return $this->applyScopes($policies);
    }

    /**
     * Optional method if you want to use html builder.
     *
     * @return \Yajra\Datatables\Html\Builder
     */
    public function html()
    {
        return $this->builder()
            ->columns($this->getColumns())
            ->addAction(['width' => '10%'])
            ->ajax('')
            ->parameters([
                'dom' => 'Bfrtip',
                'scrollX' => false,
                'buttons' => [
                    'create',
                    'print',
                    'reset',
                    'reload',
                    [
                         'extend'  => 'collection',
                         'text'    => '<i class="fa fa-download"></i> Export',
                         'buttons' => [
                             'csv',
                             'excel',
                         ],
                    ],
                    'colvis'
                ]
            ]);
    }

    /**
     * Get columns.
     *
     * @return array
     */
    private function getColumns()
    {
        return [
            'name' => ['name' => 'name', 'data' => 'name'],
            'description' => ['name' => 'description', 'data' => 'description']
        ];
    }

    /**
     * Get filename for export.
     *
     * @return string
     */
    protected function filename()
    {
        return 'policies';
    }
}

GET http://localhost:8000/policies?draw=1&columns%5B0%5D%5Bdata%5D=name&columns%5B0%5D%5Bname%5D=name&columns%5B0%5D%5Bsearchable%5D=true&columns%5B0%5D%5Borderable%5D=true&columns%5B0%5D%5Bsearch%5D%5Bvalue%5D=&columns%5B0%5D%5Bsearch%5D%5Bregex%5D=false&columns%5B1%5D%5Bdata%5D=description&columns%5B1%5D%5Bname%5D=description&columns%5B1%5D%5Bsearchable%5D=true&columns%5B1%5D%5Borderable%5D=true&columns%5B1%5D%5Bsearch%5D%5Bvalue%5D=&columns%5B1%5D%5Bsearch%5D%5Bregex%5D=false&columns%5B2%5D%5Bdata%5D=action&columns%5B2%5D%5Bname%5D=action&columns%5B2%5D%5Bsearchable%5D=false&columns%5B2%5D%5Borderable%5D=false&columns%5B2%5D%5Bsearch%5D%5Bvalue%5D=&columns%5B2%5D%5Bsearch%5D%5Bregex%5D=false&order%5B0%5D%5Bcolumn%5D=0&order%5B0%5D%5Bdir%5D=asc&start=0&length=10&search%5Bvalue%5D=&search%5Bregex%5D=false&_=1506164857095 500 (Internal Server Error)

yajra commented 7 years ago

Please see upgrade guide as there are breaking changes.

yajra commented 7 years ago
return $this->datatables
            ->eloquent($this->query())

Should be replace to something like:

return datatables()->eloquent($this->query())...
aescasio commented 7 years ago

Hi @yajra

Thanks so much for enlighten things around.

But before that, to solve things around I use the code below that output pretty the same.

use Yajra\DataTables\EloquentDataTable;

$dataTable = new EloquentDataTable($query); return $dataTable->addColumn('action', 'someviews.datatables_actions');