【问题标题】:DataTables warning: table id=DataTables_Table_0 - Ajax errorDataTables 警告:表 id=DataTables_Table_0 - Ajax 错误
【发布时间】:2017-08-17 12:44:10
【问题描述】:

我在 laravel 中使用了 Datatables 并收到以下警告消息:

DataTables 警告:表 id=DataTables_Table_0 - Ajax 错误。更多 有关此错误的信息,请参阅http://datatables.net/tn/7

我无法从数据库中的表中获取 JSON 数据

这里是我的控制器

 public function data()
    {
       // return Datatables::of(Employee::select("*"))->make(true);
        $users = Employee::select(['id','employee_name','gender', 'join_date', 'identity_no', 'birthdate', 'idaddress']);
        return Datatables::of($users)->make();
    }

路线

Route::any("data", "EmployeeController@data");

型号

 public function attributeLabels()
    {
        return [
            "id" => "id",
            "nik" => "Nik",
            "employee_name" => "Nama Pegawai",
            "gender" => "Jenis kelamin",
            "join_date" => "Tgl bergabung",
            "identity_no" => "No identitas",
            "birthdate" => "Tgl Lahir",
            "idaddress" => "Alamat",
        ];
    }

表格

<table class="table table-bordered" datatable="{{ url("hr/employee/data") }}">
    <thead>
      <tr>
        <th dt-field="id"> {{ $model->label("id") }} </th>
        <th dt-field="nik"> {{ $model->label("nik") }} </th>
        <th dt-field="employee_name"> {{ $model->label("employee_name") }} </th>
        <th dt-field="gender"> {{ $model->label("gender") }} </th>
        <th dt-field="join_date"> {{ $model->label("join_date") }} </th>
        <th dt-field="identity_no"> {{ $model->label("identity_no") }} </th>
        <th dt-field="birthdate"> {{ $model->label("birthdate") }} </th>
        <th dt-field="idaddress"> {{ $model->label("idaddress") }} </th>
        <th dt-col="#dt-action" sort="false" search="false"> </th>
      </tr>
    </thead>

我哪里做错了?我该如何解决这个问题?

【问题讨论】:

    标签: php mysql ajax laravel datatable


    【解决方案1】:

    我认为您错过了在选择查询中选择 nik。如果这样做,它应该工作。当数据表中缺少任何字段时,它会抛出此错误。

    【讨论】:

    • 即使我用了 eloquent 还是会报错return Datatables::of(Employee::select("*"))-&gt;make(true);
    • 只需在下面的代码行中添加 nik 字段即可。 $users = Employee::select(['id','employee_name','gender', 'join_date', 'identity_no', 'birthdate', 'idaddress']);
    猜你喜欢
    • 2021-10-19
    • 1970-01-01
    • 2019-12-14
    • 1970-01-01
    • 1970-01-01
    • 2014-07-04
    • 1970-01-01
    • 1970-01-01
    • 2020-03-11
    相关资源
    最近更新 更多