0

我一直在 laravel 中使用 yajra dataable。我在渲染表格时遇到了问题。我正在尝试将数组传递给数据表 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 do 中传递 true 的原因。请提供此问题的解决方案。

4

1 回答 1

2

无需在数组内添加值。将模型直接发送到数据表中。

$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},
     ],
});
于 2018-09-11T07:34:39.490 回答