I am unable to get my yajrabox-datatable to render in my view. I get the following error:

DataTables warning: table id=dataTableBuilder - Invalid JSON response. For more information about this error, please see http://datatables.net/tn/1

I have set extended the datatables class and included return $dataTable->render('activities/index'); in my controller.

ActivitiesController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Activity;
use DB;
use Yajra\Datatables\Datatables;
use Redirect,Response;
Use App\DataTables\ActivityDataTable;
use Session;
use Log;


class ActivitiesController extends Controller
{
    public function index(ActivityDataTable $dataTable)
    {


        session(['source' => 'activities']);
        Log::info('Visiting: index');
        Log::info('Source: '.session('source'));

        return $dataTable->render('activities/index');
    }
}

ActivityDataTable.php

<?php

namespace App\DataTables;

use App\Activity;
use Yajra\DataTables\Services\DataTable;
use Yajra\DataTables\EloquentDataTable;
use Yajra\DataTables\DataTables;

class ActivityDataTable extends DataTable
{


    /**
    * Display ajax response.
    *
    * @return \Illuminate\Http\JsonResponse
    */
    public function ajax()
    {
        return $this->datatables
            ->eloquent($this->query())
            ->make(true);
    }


    /**
     * Build DataTable class.
     *
     * @param mixed $query Results from query() method.
     * @return \Yajra\DataTables\DataTableAbstract
     */
    public function dataTable($query, DataTables $dataTables) 
    {
        return $dataTables->eloquent($query);
    }

    /**
     * Get query source of dataTable.
     *
     * @param \App\Activity $model
     * @return \Illuminate\Database\Eloquent\Builder
     */
    public function query()
    {
        // $query=Activity::all()->take(50);

        // return Datatables::of($query)
        //     ->addColumn('user', function ($query) {
        //         return $query->user->name;
        //     })->make(true);

        return Activity::query();

    }

    /**
     * Optional method if you want to use html builder.
     *
     * @return \Yajra\DataTables\Html\Builder
     */
    public function html()
    {
        return $this->builder()
                    ->columns($this->getColumns())
                    ->parameters($this->getBuilderParameters());
    }


 /**
     * Get parameters.
     *
     * @return array
     */
    protected function getBuilderParameters()
    {
        return [
            'dom'          => 'Bfrtip',
            'buttons'      => ['excel'],
        ];
    }


    /**
     * Get columns.
     *
     * @return array
     */
    protected function getColumns()
    {
        return [
            'id',
            'month',
            'activity',
            'learned',
            'role',
            'hours',
            'user',
        ];
    }

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

1 Answers

0
GlenUK On

Okay,

I was using a guide that was for an older version of yajra-datatables.

Following this upgrade guide resolved my issue:

https://yajrabox.com/docs/laravel-datatables/7.0/upgrade