如何使用Laravel 5.4显示来自Datatables的数据?

如何使用Laravel 5.4显示来自Datatables的数据?

问题描述:

I'm stuck with datatables and the plugin yajrabox for Laravel 5.4. The goal is to load with ajax and the plugin the data from the users tables in my database, but it just shows me this error :

DataTables warning: table id=listingUsers - Requested unknown parameter '0' for row 0, column 0. 

I can't find out where it's coming from and i'm not sure I wrote the code right..

Here is my code.

Controller :

 public function index() {
    $users = User::latest()->count();

    return view('admin.users.index', compact('users'));
}

 public function ajaxListing() {
    $users = User::select(['id', 'username', 'email']);
        return Datatables::of($users)->make(true);
}

Routes :

Route::resource('users', 'Admin\UsersController');
Route::any('user-data', 'Admin\UsersController@ajaxListing')->name('datatables.data');

View : `

<table class="table table-bordered table-responsive" id="listingUsers">
    <thead>
        <th>ID</th>
        <th>Nom</th>
        <th>Email</th>
    </thead>

    <tbody></tbody>
</table>

@push('scripts')
<script>
    $(document).ready(function () {
        $('#listingUsers').DataTable({
            processing: true,
            serverSide: true,
            ajax: '{!! route('datatables.data') !!}',
            columns: [
                {data: 0, name: 'id'},
                {data: 1, name: 'name'},
                {data: 2, name: 'email'}
            ]
        });
    });

</script>
@endpush`

WHen i'm searching the error in the console and the network the data comes right but just doesn't show in the table.

Can someone tell me what t'im doing wrong and how to fix it ?

我坚持使用数据表和Laravel 5.4的插件yajrabox。 目标是使用ajax和插件加载数据库中users表中的数据,但它只显示了这个错误: p>

  DataTables警告:table id = listingUsers  -  第0行,第0列请求未知参数'0'。
  code>  pre> 
 
 

我无法找到它来自哪里,我不确定我是否编写了代码 对.. p>

这是我的代码。 p>

控制器: p>

 公共功能索引(  ){
 $ users = User :: latest() - &gt; count(); 
 
返回视图('admin.users.index',compact('users')); 
} 
 
  public function ajaxListing(){
 $ users = User :: select(['id','username','email']); 
返回Datatables :: of($ users) - &gt; make(true);  
} 
  code>  pre> 
 
 

路由: p>

  Route :: resource('users','Admin \ UsersController'  ); 
Route :: any('user-data','Admin \ UsersController @ ajaxListing') - &gt; name('datatables.data'); 
  code>  pre> 
 
 

查看:` p>

 &lt; table class =“table table-bordered table-responsive”id =“listin  gUsers“&gt; 
&lt; thead&gt; 
&lt; th&gt; ID&lt; / th&gt; 
&lt; th&gt; Nom&lt; / th&gt; 
&lt; th&gt;电子邮件&lt; / th&gt; 
&lt; / thead&gt;  
 
&lt; tbody&gt;&lt; / tbody&gt; 
&lt; / table&gt; 
 
 @ push('scripts')
&lt; script&gt; 
 $(document).ready(function(){
  $('#listingUsers')。DataTable({
处理:true,
 serverSide:true,
 ajax:'{!!  route('datatables.data')!!}',
列:[
 {data:0,name:'id'},
 {data:1,name:'name'},
 {data  :2,名称:'email'} 
] 
}); 
}); 
 
&lt; / script&gt; 
 @ endpush` 
  code>  pre> 
 
 当我在控制台和网络中搜索错误时数据是正确的但是没有在表格中显示。 p> 
 
 

有人能告诉我这是错误的吗? 以及如何解决它? p> div>

In data, you need to specify the column name, like that:

columns: [
    {data: 'id', name: 'id'},
    {data: 'username', name: 'username'},
    {data: 'email, name: 'email'}
]