Laravel-datatables: Filter not working

Created on 4 Feb 2017  ·  3Comments  ·  Source: yajra/laravel-datatables

  public function getincome_ajax(Request $request)
        {
            $starts = $request->start;
            $ende = $request->end;
            $course = $request->course;

                $start = date('Y-d-m', strtotime($starts));
                $end = date('Y-d-m', strtotime($ende));


            if (($starts) && ($ende) && ($course))
             {
                             //here is problem when i want search with $starts and $end its not working
                $query = Subscription::whereDate('created_at','>=',$start)
                                                ->whereDate('created_at','<=',$end)
                                                ->where('course_id',$course)
                                                ->with('courses');
            }
             elseif(($starts) && ($ende))
             {
                                  //here is problem when i want search with $starts and $end its not working
                $query = Subscription::whereDate('created_at','>=',$start)
                                                ->whereDate('created_at','<=',$end)
                                                ->with('courses');

         }

             elseif($course)
             {
                                    //here if I search with only $course its working fine
                $query = Subscription::where('course_id',$course)->with('courses');

             }

            else
            {
                         $query = Subscription::with('courses');
            }

            $dataTable = Datatables::of($query)
      ->addColumn('total', function ($s) {
                 return $s->tution_fee + $s->development_fee + $s->late_fee + $s->other_fee ;
               })
                             ->editColumn('created_at', function ($user) {
                             return $user->created_at->format('d/m/Y');
                     });
       $columns = ['created_at', 'tution_fee', 'development_fee','late_fee', 'other_fee','total','courses.name'];
      $base = new DataTableBase($query, $dataTable, $columns);
      return $base->render(null);
        }

script

   <script src="/vendor/datatables/buttons.server-side.js"></script>
      <script src="/js/bootstrap-datepicker.min.js"></script>

       <script type="text/javascript">
          var oTable = $('#userstable').DataTable({
            processing: true,
            serverSide: true,
            dom: 'Bfrtip',
                 buttons: [
                     'copy', 'csv', 'excel', 'pdf', 'print'
                 ],
               processing: true,
               serverSide: true,
               ajax: {
                   url: '/acadmic/income/search_ajax',
                   data: function (d) {

                       d.course = $('select[name=course]').val();
                       d.start = $('input[name=start]').val();
                       d.end = $('input[name=end]').val();

                   }
               },
               columns: [
                            { data: 'id', name: 'id' },
                             { data: 'created_at', name: 'created_at' },
                             { data: 'tution_fee', name: 'tution_fee' },
                             {data: 'development_fee', name: 'development_fee'},
                             { data: 'late_fee', name: 'late_fee' },
                             { data: 'other_fee', name: 'other_fee' },
                             {data: 'total', name: 'total', orderable: false, searchable: false},
                             { data: 'courses.name', name: 'courses.name' },
               ]
           });

           $('#search-form').on('submit', function(e) {
               oTable.draw();
               e.preventDefault();
           });
       </script>

   <script type="text/javascript">

      $(document).ready(function(){
        $('#sandbox-container .input-daterange').datepicker({
    format: "dd/mm/yyyy",
    forceParse: false,
    autoclose: true
});
      })
  </script>
question

All 3 comments

You should not use start as a field name on your request because it is being used by dataTables when paging the results.

yes i changed that and its working and also my if statement date format problem i changed and it's working

@yajra You save my day! Thx!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

AbuHamdah picture AbuHamdah  ·  33Comments

FaZeRs picture FaZeRs  ·  18Comments

aliworkshop picture aliworkshop  ·  14Comments

mantrax314 picture mantrax314  ·  15Comments

ezani92 picture ezani92  ·  33Comments