Laravel 5.8 / Yajrabox-datatables-设置服务

时间:2019-05-12 10:43:56

标签: php laravel yajra-datatable

我无法获取yajrabox-datatable在我的视图中呈现。我收到以下错误:

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

我设置了扩展数据表类,并将return $dataTable->render('activities/index');包含在控制器中。

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 个答案:

答案 0 :(得分:0)

好吧

我使用的指南适用于旧版本的yajra-datatables。

遵循此升级指南解决了我的问题:

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