Search code examples
datatableslaravel-5.3

How to load more data using the Laravel in tabular form


I have daily data of 500 rows nearly and I have to show in tabular form the data for 4 years. I am using Laravel and putting the output in Datatable. The data hangs itself in the ajax data itself. How to proceed in this, so I can show data in some chunks(pagination) easily? Please help by giving some example link.


Solution

  • I normally use laravel datatable for this type of task. It's straight forward and saves you from stress. https://github.com/yajra/laravel-datatables

     public  static function user_list(){
    
            $users = User::select(['id','name', 'email','created_at'])->with('profile')->get();
            return Datatables::of($users)
    
                ->addColumn('roles', function ($users){
                    return $users->roles->values()->implode('name',',');
                })
                ->addColumn('Phone', function ($users){
    
                       return $users->phone;
                   }
                })
    
                ->addColumn('Sex', function ($users){
                    return $users->sex
                })
                ->addColumn('Edit', function ($users){
                return $users->id;
                })
                ->addColumn('Delete', function ($users){
                    return $users->id;
                })
    
    
    
                ->make();
    
        }
    

    And in my view

     <script>
                    $(document).ready(function () {
                        var table = $('#datatable').DataTable({
                            processing: true,
                            serverSide: true,
                            ajax: '{!! url('user/datatable/list') !!}',
                            "lengthMenu": [[50, 100, 500, 1000, -1], [50, 100, 500, 1000, "All"]],
    
                            dom: 'Bfrtip',
                            buttons: [
                                 'excel', 'pdf', 'print',
                                'copyHtml5',
                                'excelHtml5',
                                'csvHtml5',
                                'pdfHtml5',
                                'colvis'
                            ],
                            'columnDefs': [{
                                'targets': 0,
                                'searchable': false,
                                'orderable': false,
                                'className': 'dt-body-center',
                    });
    
                </script>