Yajra Datatables Laravel

- 1 answer

I am having issues using Yajra Datatables in my Laravel App.

I have followed a online guide however I get a blank response. I need to display a list of members based on their points.

This is the Table in the View Page:

                    <div class="table-responsive">
                        <table class="table table-striped table-no-boreded table-hover" id="points-table">
                            <thead class = "text-primary">
                            <h4> </h4>
                                    <th class="text-center">Postion</th>
                                    <th class="text-center">Member</th>
                                    <th class="text-center">Total Points</th>
                                    <th width="10%"></th>

This the Script on the same view page

<script src=""></script>
<script src=""></script>
<script src=""></script>
<script src=""></script>
<script src=""></script>

<script type = "text/javascript">
    $(function() {

        var table=$('.points').DataTable({
            processing: true,
            serverSide: true,
            ajax: '{{ route('getPoints') }}',
            columns: [
                { data: 'DT_RowIndex', name: 'DT_RowIndex' },
                { data: 'member_id', name: 'member' },
                { data: 'total_points', name: 'Total Points' },
                { data: 'action', name: 'action', orderable: true, searchable: true },


This is the Controller

  public function getPoints(Request $request)
        $year =  Carbon::parse(Carbon::now())->year;

        if ($request->ajax()) {

            $points = Points::query()
            ->select('member_id')->selectRaw('SUM(value) as TotalPoints')
            ->where('Year','=', $year)

            return Datatables::of($points)

I have added the get in the Routes

Route::get('get/points', '[email protected]')->middleware('auth')->name('getPoints');

I know my query works, as when I had a normal data this query worked fine. Yet under the code listed above, I don't get a reponse.

I have 1 record in my points table, therefore I should see something under the datatable.

Any help to point me in the right direction would be fantastic



You are passing wrong class in your javascript code.

Instead of $('.points') class you need to put $('#points-table')

var table=$('#points-table').DataTable({
  // your code

Put code in your controller like

return DataTables::of($points)