laravel Yajra Datatable
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了laravel Yajra Datatable相关的知识,希望对你有一定的参考价值。
我一直在laravel中使用yajra datable。我在渲染表时面临一个问题。我试图将数组传递给datatable make方法。但是会引发错误。以下是laravel中的代码。
$tableData = array();
$users = User::all();
foreach($users as $user) {
$tableData['id']=$user->id;
$tableData['name']=$user->name;
$tableData['email']=$user->email;
$tableData['role']=$user->roles->first()->toArray()['name'];
}
return Datatables::of($tableData)->make(true);
下面是javascript中数据表的代码
$('.users').DataTable({
processing: true,
serverSide: true,
ajax: "users/list",
lengthMenu: [10, 20, 40, 60, 80, 100],
pageLength: 10,
columnDefs: [{
targets: [0, 1, 2],
className: 'mdl-data-table__cell--non-numeric'
}]
});
以下是我在浏览器控制台中收到的错误
{
"message": "Call to a member function getQuery() on array",
"exception": "Symfony\Component\Debug\Exception\FatalThrowableError",
"file": "E:\projects\htdocs\whm\vendor\yajra\laravel-datatables-oracle\src\Engines\EloquentEngine.php",
"line": 39,
"trace": [
{
"file": "E:\projects\htdocs\whm\vendor\yajra\laravel-datatables-oracle\src\Datatables.php",
"line": 96,
"function": "__construct",
"class": "Yajra\Datatables\Engines\EloquentEngine",
"type": "->"
},
{
"file": "E:\projects\htdocs\whm\vendor\yajra\laravel-datatables-oracle\src\Datatables.php",
"line": 60,
"function": "usingEloquent",
"class": "Yajra\Datatables\Datatables",
"type": "->"
},
{
"file": "E:\projects\htdocs\whm\vendor\laravel\framework\src\Illuminate\Support\Facades\Facade.php",
"line": 223,
"function": "of",
"class": "Yajra\Datatables\Datatables",
"type": "::"
},
{
"file": "E:\projects\htdocs\whm\app\Http\Controllers\UserController.php",
"line": 44,
"function": "__callStatic",
"class": "Illuminate\Support\Facades\Facade",
"type": "::"
},
{
"function": "list",
"class": "App\Http\Controllers\UserController",
"type": "->"
},
{
"file": "E:\projects\htdocs\whm\vendor\laravel\framework\src\Illuminate\Routing\Controller.php",
"line": 54,
"function": "call_user_func_array"
},
{
"file": "E:\projects\htdocs\whm\vendor\laravel\framework\src\Illuminate\Routing\ControllerDispatcher.php",
"line": 45,
"function": "callAction",
"class": "Illuminate\Routing\Controller",
"type": "->"
},
{
"file": "E:\projects\htdocs\whm\vendor\laravel\framework\src\Illuminate\Routing\Route.php",
"line": 212,
"function": "dispatch",
"class": "Illuminate\Routing\ControllerDispatcher",
"type": "->"
},
{
"file": "E:\projects\htdocs\whm\vendor\laravel\framework\src\Illuminate\Routing\Route.php",
"line": 169,
"function": "runController",
"class": "Illuminate\Routing\Route",
"type": "->"
},
{
"file": "E:\projects\htdocs\whm\vendor\laravel\framework\src\Illuminate\Routing\Router.php",
"line": 679,
"function": "run",
"class": "Illuminate\Routing\Route",
"type": "->"
},
{
"file": "E:\projects\htdocs\whm\vendor\laravel\framework\src\Illuminate\Routing\Pipeline.php",
"line": 30,
"function": "Illuminate\Routing\{closure}",
"class": "Illuminate\Routing\Router",
"type": "->"
},
{
"file": "E:\projects\htdocs\whm\vendor\laravel\framework\src\Illuminate\Auth\Middleware\Authenticate.php",
"line": 43,
"function": "Illuminate\Routing\{closure}",
"class": "Illuminate\Routing\Pipeline",
"type": "->"
},
{
"file": "E:\projects\htdocs\whm\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php",
"line": 151,
"function": "handle",
"class": "Illuminate\Auth\Middleware\Authenticate",
"type": "->"
},
{
"file": "E:\projects\htdocs\whm\vendor\laravel\framework\src\Illuminate\Routing\Pipeline.php",
"line": 53,
"function": "Illuminate\Pipeline\{closure}",
"class": "Illuminate\Pipeline\Pipeline",
"type": "->"
},
{
"file": "E:\projects\htdocs\whm\vendor\laravel\framework\src\Illuminate\Routing\Middleware\SubstituteBindings.php",
"line": 41,
"function": "Illuminate\Routing\{closure}",
"class": "Illuminate\Routing\Pipeline",
"type": "->"
},
{
"file": "E:\projects\htdocs\whm\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php",
"line": 151,
"function": "handle",
"class": "Illuminate\Routing\Middleware\SubstituteBindings",
"type": "->"
},
{
"file": "E:\projects\htdocs\whm\vendor\laravel\framework\src\Illuminate\Routing\Pipeline.php",
"line": 53,
"function": "Illuminate\Pipeline\{closure}",
"class": "Illuminate\Pipeline\Pipeline",
"type": "->"
},
{
"file": "E:\projects\htdocs\whm\vendor\laravel\framework\src\Illuminate\Foundation\Http\Middleware\VerifyCsrfToken.php",
"line": 75,
"function": "Illuminate\Routing\{closure}",
"class": "Illuminate\Routing\Pipeline",
"type": "->"
},
{
"file": "E:\projects\htdocs\whm\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php",
"line": 151,
"function": "handle",
"class": "Illuminate\Foundation\Http\Middleware\VerifyCsrfToken",
"type": "->"
},
{
"file": "E:\projects\htdocs\whm\vendor\laravel\framework\src\Illuminate\Routing\Pipeline.php",
"line": 53,
"function": "Illuminate\Pipeline\{closure}",
"class": "Illuminate\Pipeline\Pipeline",
"type": "->"
},
{
"file": "E:\projects\htdocs\whm\vendor\laravel\framework\src\Illuminate\View\Middleware\ShareErrorsFromSession.php",
"line": 49,
"function": "Illuminate\Routing\{closure}",
"class": "Illuminate\Routing\Pipeline",
"type": "->"
},
{
"file": "E:\projects\htdocs\whm\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php",
"line": 151,
"function": "handle",
"class": "Illuminate\View\Middleware\ShareErrorsFromSession",
"type": "->"
},
{
"file": "E:\projects\htdocs\whm\vendor\laravel\framework\src\Illuminate\Routing\Pipeline.php",
"line": 53,
"function": "Illuminate\Pipeline\{closure}",
"class": "Illuminate\Pipeline\Pipeline",
"type": "->"
},
{
"file": "E:\projects\htdocs\whm\vendor\laravel\framework\src\Illuminate\Session\Middleware\StartSession.php",
"line": 63,
"function": "Illuminate\Routing\{closure}",
"class": "Illuminate\Routing\Pipeline",
"type": "->"
},
{
"file": "E:\projects\htdocs\whm\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php",
"line": 151,
"function": "handle",
"class": "Illuminate\Session\Middleware\StartSession",
"type": "->"
},
{
"file": "E:\projects\htdocs\whm\vendor\laravel\framework\src\Illuminate\Routing\Pipeline.php",
"line": 53,
"function": "Illuminate\Pipeline\{closure}",
"class": "Illuminate\Pipeline\Pipeline",
"type": "->"
},
{
"file": "E:\projects\htdocs\whm\vendor\laravel\framework\src\Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse.php",
"line": 37,
"function": "Illuminate\Routing\{closure}",
"class": "Illuminate\Routing\Pipeline",
"type": "->"
},
{
"file": "E:\projects\htdocs\whm\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php",
"line": 151,
"function": "handle",
"class": "Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse",
"type": "->"
},
{
"file": "E:\projects\htdocs\whm\vendor\laravel\framework\src\Illuminate\Routing\Pipeline.php",
"line": 53,
"function": "Illuminate\Pipeline\{closure}",
"class": "Illuminate\Pipeline\Pipeline",
"type": "->"
},
{
"file": "E:\projects\htdocs\whm\vendor\laravel\framework\src\Illuminate\Cookie\Middleware\EncryptCookies.php",
"line": 66,
"function": "Illuminate\Routing\{closure}",
"class": "Illuminate\Routing\Pipeline",
"type": "->"
},
{
"file": "E:\projects\htdocs\whm\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php",
"line": 151,
"function": "handle",
"class": "Illuminate\Cookie\Middleware\EncryptCookies",
"type": "->"
},
{
"file": "E:\projects\htdocs\whm\vendor\laravel\framework\src\Illuminate\Routing\Pipeline.php",
"line": 53,
"function": "Illuminate\Pipeline\{closure}",
"class": "Illuminate\Pipeline\Pipeline",
"type": "->"
},
{
"file": "E:\projects\htdocs\whm\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php",
"line": 104,
"function": "Illuminate\Routing\{closure}",
"class": "Illuminate\Routing\Pipeline",
"type": "->"
},
{
"file": "E:\projects\htdocs\whm\vendor\laravel\framework\src\Illuminate\Routing\Router.php",
"line": 681,
"function": "then",
"class": "Illuminate\Pipeline\Pipeline",
"type": "->"
},
{
"file": "E:\projects\htdocs\whm\vendor\laravel\framework\src\Illuminate\Routing\Router.php",
"line": 656,
"function": "runRouteWithinStack",
"class": "Illuminate\Routing\Router",
"type": "->"
},
{
"file": "E:\projects\htdocs\whm\vendor\laravel\framework\src\Illuminate\Routing\Router.php",
"line": 622,
"function": "runRoute",
"class": "Illuminate\Routing\Router",
"type": "->"
},
{
"file": "E:\projects\htdocs\whm\vendor\laravel\framework\src\Illuminate\Routing\Router.php",
"line": 611,
"function": "dispatchToRoute",
"class": "Illuminate\Routing\Router",
"type": "->"
},
{
"file": "E:\projects\htdocs\whm\vendor\laravel\framework\src\Illuminate\Foundation\Http\Kernel.php",
"line": 176,
"function": "dispatch",
"class": "Illuminate\Routing\Router",
"type": "->"
},
{
"file": "E:\projects\htdocs\whm\vendor\laravel\framework\src\Illuminate\Routing\Pipeline.php",
"line": 30,
"function": "Illuminate\Foundation\Http\{closure}",
"class": "Illuminate\Foundation\Http\Kernel",
"type": "->"
},
{
"file": "E:\projects\htdocs\whm\vendor\fideloper\proxy\src\TrustProxies.php",
"line": 57,
"function": "Illuminate\Routing\{closure}",
"class": "Illuminate\Routing\Pipeline",
"type": "->"
},
{
"file": "E:\projects\htdocs\whm\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php",
"line": 151,
"function": "handle",
"class": "Fideloper\Proxy\TrustProxies",
"type": "->"
},
{
"file": "E:\projects\htdocs\whm\vendor\laravel\framework\src\Illuminate\Routing\Pipeline.php",
"line": 53,
"function": "Illuminate\Pipeline\{closure}",
"class": "Illuminate\Pipeline\Pipeline",
"type": "->"
},
{
"file": "E:\projects\htdocs\whm\vendor\laravel\framework\src\Illuminate\Foundation\Http\Middleware\TransformsRequest.php",
"line": 31,
"function": "Illuminate\Routing\{closure}",
"class": "Illuminate\Routing\Pipeline",
"type": "->"
},
{
"file": "E:\projects\htdocs\whm\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php",
"line": 151,
"function": "handle",
"class": "Illuminate\Foundation\Http\Middleware\TransformsRequest",
"type": "->"
},
{
"file": "E:\projects\htdocs\whm\vendor\laravel\framework\src\Illuminate\Routing\Pipeline.php",
"line": 53,
"function": "Illuminate\Pipeline\{closure}",
"class": "Illuminate\Pipeline\Pipeline",
"type": "->"
},
{
"file": "E:\projects\htdocs\whm\vendor\laravel\framework\src\Illuminate\Foundation\Http\Middleware\TransformsRequest.php",
"line": 31,
"function": "Illuminate\Routing\{closure}",
"class": "Illuminate\Routing\Pipeline",
"type": "->"
},
{
"file": "E:\projects\htdocs\whm\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php",
"line": 151,
"function": "handle",
"class": "Illuminate\Foundation\Http\Middleware\TransformsRequest",
"type": "->"
},
{
"file": "E:\projects\htdocs\whm\vendor\laravel\framework\src\Illuminate\Routing\Pipeline.php",
"line": 53,
"function": "Illuminate\Pipeline\{closure}",
"class": "Illuminate\Pipeline\Pipeline",
"type": "->"
},
{
"file": "E:\projects\htdocs\whm\vendor\laravel\framework\src\Illuminate\Foundation\Http\Middleware\ValidatePostSize.php",
"line": 27,
"function": "Illuminate\Routing\{closure}",
"class": "Illuminate\Routing\Pipeline",
"type": "->"
},
{
"file": "E:\projects\htdocs\whm\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php",
"line": 151,
"function": "handle",
"class": "Illuminate\Foundation\Http\Middleware\ValidatePostSize",
"type": "->"
},
{
"file": "E:\projects\htdocs\whm\vendor\laravel\framework\src\Illuminate\Routing\Pipeline.php",
"line": 53,
"function": "Illuminate\Pipeline\{closure}",
"class": "Illuminate\Pipeline\Pipeline",
"type": "->"
},
{
"file": "E:\projects\htdocs\whm\vendor\laravel\framework\src\Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode.php",
"line": 62,
"function": "Illuminate\Routing\{closure}",
"class": "Illuminate\Routing\Pipeline",
"type": "->"
},
{
"file": "E:\projects\htdocs\whm\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php",
"line": 151,
"function": "handle",
"class": "Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode",
"type": "->"
},
{
"file": "E:\projects\htdocs\whm\vendor\laravel\framework\src\Illuminate\Routing\Pipeline.php",
"line": 53,
"function": "Illuminate\Pipeline\{closure}",
"class": "Illuminate\Pipeline\Pipeline",
"type": "->"
},
{
"file": "E:\projects\htdocs\whm\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php",
"line": 104,
"function": "Illuminate\Routing\{closure}",
"class": "Illuminate\Routing\Pipeline",
"type": "->"
},
{
"file": "E:\projects\htdocs\whm\vendor\laravel\framework\src\Illuminate\Foundation\Http\Kernel.php",
"line": 151,
"function": "then",
"class": "Illuminate\Pipeline\Pipeline",
"type": "->"
},
{
"file": "E:\projects\htdocs\whm\vendor\laravel\framework\src\Illuminate\Foundation\Http\Kernel.php",
"line": 116,
"function": "sendRequestThroughRouter",
"class": "Illuminate\Foundation\Http\Kernel",
"type": "->"
},
{
"file": "E:\projects\htdocs\whm\public\index.php",
"line": 55,
"function": "handle",
"class": "Illuminate\Foundation\Http\Kernel",
"type": "->"
}
]
}
另外,请指定make中传递的内容。请提供此问题的解决方案。
答案
无需在数组中添加值。直接将模型发送到Datatables。
$users = User::all();
return Datatables::of($users)
->editColumn('role', function ($user) {
return $user->roles->first()->name;
})
->make(true);
至于你的JS部分
$('.users').DataTable({
processing: true,
serverSide: true,
ajax: "users/list",
lengthMenu: [10, 20, 40, 60, 80, 100],
pageLength: 10,
columnDefs: [{
targets: [0, 1, 2],
className: 'mdl-data-table__cell--non-numeric'
}],
"columns": [
{ data: 'index', defaultContent: '', searchable: false, render: function (data, type, row, meta) {
return meta.row + meta.settings._iDisplayStart + 1; //auto increment
}},
{ data: "name", name: "name"},
{ data: "email", name: "email"},
{ data: "role", name: "role", searchable: false},
],
});
以上是关于laravel Yajra Datatable的主要内容,如果未能解决你的问题,请参考以下文章
在 Laravel 8 中使用 yajra 数据表加载 DataTable where select
Laravel Yajra Datatable 问题与分页和 recordsFiltered
Laravel,Yajra Datatable,如果 y 那么如何返回 Yes else No
编辑列日期格式后不显示过滤日期 - Laravel Yajra Datatable