Yajra Datatables Package for Laravel deosnt work properly with laravel 5.2

故事扮演 提交于 2019-12-13 02:01:32

问题


I have installed yajra/laravel-datatables-oracle "~6.0" package for supporting server-side datatables in laravel 5.2 with MySql as database. i'm trying to display the datatable of users:

//routes.php
Route::group(['middleware' => ['web'], 'prefix' => 'user'], function () {
    Route::get('/index', 'UserController@index')->name('user.index');
});

and here is my controller:

//UserController.php
public function index()
{
    return view('user.index');
}

public function indexData()
{
    $users = User::select(['id', 'name', 'email', 'created_at', 'updated_at'])->get();
    return Datatables::of($users)->make();
}

the view:

// user\index.blade.php
@extends('layouts.base')

@section('additional_styles')
    <link rel="stylesheet" href="//cdn.datatables.net/1.10.7/css/jquery.dataTables.min.css">
@endsection

@section('additional_scripts')
    <!-- DataTables -->
    <script src="//cdn.datatables.net/1.10.7/js/jquery.dataTables.min.js"></script>
    <script>
        $('#users-table').DataTable({
            "processing": true,
            "serverSide": true,
            "ajax": '{!! route('user.index') !!}',
            "columns": [
                {data: 'id', name: 'id'},
                {data: 'name', name: 'name'},
                {data: 'email', name: 'email'},
                {data: 'created_at', name: 'created_at'},
                {data: 'updated_at', name: 'updated_at'}
            ]
        });
    </script>
@endsection


@section('main-content')
    <div class="container">
        <div class="row">
            <div class="col-md-10 col-md-offset-1">
                <div class="panel panel-default">
                    <div class="panel-body">
                        <table class="table table-bordered" id="users-table">
                            <thead>
                            <tr>
                                <th>Id</th>
                                <th>Name</th>
                                <th>Email</th>
                                <th>Created At</th>
                                <th>Updated At</th>
                            </tr>
                            </thead>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>
@endsection

but the datatable doesn't work properly. after processing to render tha data, it show me an alert that the response have invalid JSON format and says to see datatables.net/tn/1. I have tried to see whithin the developer tools of chrome to see the response but i couldnt!

any idea about the issue ?


回答1:


now its work in laravel 5.2.31,

`        Route
    Route::get('datatables',['uses'=>'DatatablesController@getIndex', 'as' => 'datatables']);
    Route::get('datatables/{data}',['uses'=>'DatatablesController@anyData', 'as' => 'datatables.data']);

    Controller
      public function anyData()
        {
           $users = User::select(['id', 'name', 'email', 'created_at', 'updated_at'])->get();
        return Datatables::of($users)->make();
        }

    JS
    $('#users-table').DataTable({
            processing: true,
            serverSide: true,
            ajax: '{!! url('datatables/data') !!}'
        });`


来源:https://stackoverflow.com/questions/36236671/yajra-datatables-package-for-laravel-deosnt-work-properly-with-laravel-5-2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!