简体   繁体   中英

How can I show data from Datatables with Laravel 5.4?

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 ?

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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