Laravel 5.8 / Yajrabox-datatables - 设置服务
Laravel 5.8 / Yajrabox-datatables - Setting up a service
我无法让我的 yajrabox-datatable 在我的视图中呈现。我收到以下错误:
DataTables warning: table id=dataTableBuilder - Invalid JSON response. For more information about this error, please see http://datatables.net/tn/1
我已设置扩展数据表 class 并在我的控制器中包含 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');
}
}
好的,
我使用的是旧版 yajra-datatables 的指南。
遵循此升级指南解决了我的问题:
我无法让我的 yajrabox-datatable 在我的视图中呈现。我收到以下错误:
DataTables warning: table id=dataTableBuilder - Invalid JSON response. For more information about this error, please see http://datatables.net/tn/1
我已设置扩展数据表 class 并在我的控制器中包含 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');
}
}
好的,
我使用的是旧版 yajra-datatables 的指南。
遵循此升级指南解决了我的问题: