0

I am new to laravel. I want to fetch the data from database and put it to datatable. This is my html

<div class="tab-pane" id="th_days_client" >
                        <div class="panel panel-default">
                            <div class="panel-heading">
                                <h4 class="panel-title">
                                    <i class="material-icons">flag</i>
                                    {{ $title }}
                                </h4>
                                <span class="pull-right">
                                    <i class="fa fa-fw fa-chevron-up clickable"></i>
                                    <i class="fa fa-fw fa-times removepanel clickable"></i>
                                </span>
                            </div>
                            <div class="panel-body">
                                <div class="table-responsive">
                                    <table id="th_days_client_data" class="table table-bordered">
                                        <thead>
                                        <tr>
                                            <th>Name</th>
                                            <th>Email</th>
                                            <th>Mobile</th>
                                            <th>Actions</th>
                                        </tr>
                                        </thead>
                                        <tbody>
                                        </tbody>
                                    </table>
                                </div>
                            </div>
                        </div>
                    </div>

The jQuery for this is :

var th_days_client;
    $(document).ready(function () {
        th_days_client = $('#th_days_client_data').DataTable({
            "processing": true,
            "serverSide": true,
            "order": [],
            "columns":[
                {"data":"name"},
                {"data":"email"},
                {"data":"mobile"},
                {"data":"actions"}
            ],
            "ajax": "{{ url('client') }}" + ((typeof $('#th_days_client_data').attr('data-id') != "undefined") ? "/" + $('#id').val() + "/" + $('#th_days_client_data').attr('data-id') : "/th_days_client_data")
        });
    });

controller Function for this is :

public function th_days_client_data(Datatables $datatables)
{

   $clientObj = new Client;
    $client = $clientObj->get()
        ->map(function ($client) {
        return [
            'id' => $client->id,
            'name' => $client->first_name.' '.$client->last_name,
            'email' => $client->email,
            'mobile' => $client->mobile
        ];
    });

    return $datatables->collection($client)

        ->addColumn('actions', '@if(Sentinel::inRole(\'admin\'))
                                <a href="{{ url(\'client/\' . $id . \'/edit\' ) }}" title="{{ trans(\'table.edit\') }}">
                                        <i class="fa fa-fw fa-pencil text-warning "></i> </a>
                                @endif
                                <a href="{{ url(\'client/\' . $id . \'/show\' ) }}" title="{{ trans(\'table.details\') }}" >
                                        <i class="fa fa-fw fa-eye text-primary"></i> </a>
                                @if(Sentinel::inRole(\'admin\'))
                                <a href="javascript:void(0)" onclick="deleteClient({{$id}})"  title="{{ trans(\'table.delete\') }}">
                                        <i class="fa fa-fw fa-trash text-danger"></i> </a>
                                   @endif')

        ->removeColumn('id')
        ->rawColumns(['actions'])->make();
}

When I am running this code then it is giving error :

DataTables warning: table id=th_days_client_data - Ajax error. For more information about this error, please see http://datatables.net/tn/7

How can I resolve this issue ?

4
  • And did you visit the URL mentioned? Commented Aug 1, 2018 at 8:18
  • Datatable id should be your table id : pending_client_data Commented Aug 1, 2018 at 8:22
  • yes i visited .. Commented Aug 1, 2018 at 8:25
  • @AmanKumar updated the table id but got the same error Commented Aug 1, 2018 at 8:28

2 Answers 2

1

Laravel ajax validate CSRF Token so add below code

$(document).ready(function() {

    $.ajaxSetup({
        headers: {
            'X-CSRF-TOKEN': $('meta[name="_token"]').attr('content'),
        }
    });
});
Sign up to request clarification or add additional context in comments.

Comments

0

Response status must be 2XX and response data must be valid JSON You should check it or attach response data to you question as well

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.