简体   繁体   English

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

[英]How can I show data from Datatables with Laravel 5.4?

I'm stuck with datatables and the plugin yajrabox for Laravel 5.4. 我被数据表和Laravel 5.4的插件yajrabox所困扰。 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 : 目标是使用ajax和插件加载数据库中的用户表中的数据,但这只是向我显示此错误:

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 ? 有人可以告诉我做错了什么以及如何解决?

In data , you need to specify the column name, like that: data ,您需要指定列名称,如下所示:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM