"Undefined index: data"
"Undefined index: data"
我对 Yajra\DataTables 有疑问;
当我尝试通过以下方式从控制器获取数据时:
$servives = DB::table('services')->where('status', '=', 3)->get();
return datatables($servives)->toJson();
它给我以下错误:
{"draw":1,"recordsTotal":3,"recordsFiltered":0,"data":[],"error":"Exception Message:\n\nUndefined index: data"}
在这里你可以查看我的js代码:
$(document).ready(function () {
$('#table').DataTable({
processing: true,
serverSide: true,
ajax: "{{ route('all.my_services') }}",
columns: [
{ data: 'id', name: 'id' },
{ data: 'name', name: 'name' },
{ data: 'price', name: 'price' },
{ data: 'amount', name: 'amount' },
{"mData": {},
"mRender": function (data, type, row) {
return '<a href="/partner-share-services?id='+ data.id + '"><button class="btn btn-success">Share</button></a>';
}
}
]
});
});
在这里你可以看到我的 $services 数组:
Collection {#326
#items: array:2 [
0 => {#319
+"id": 103
+"partner_id": 1004
+"name": "AI-92"
+"price": 146
+"amount": 9007
+"created_at": "2019-05-15 07:04:07"
+"updated_at": "2019-05-16 06:10:13"
+"is_active": null
+"status": 3
}
1 => {#332
+"id": 104
+"partner_id": 1004
+"name": "AI 95"
+"price": 190
+"amount": 650
+"created_at": "2019-05-16 06:49:19"
+"updated_at": "2019-05-16 06:52:34"
+"is_active": null
+"status": 3
}
]
}
怎么了?
在控制器中:
use DataTables;
use DB;
public function getDatatable(){
$services = DB::table('services')->where('status',3);
return Datatables::of($services)
->addColumn('share', function($services){
return '<a href="/partner-share-services?id='. $services->id . '"><button class="btn btn-success">Share</button></a>';
})
->rawColumns(['share'])
->make(true);
}
在路由文件中(web.php):
这里我使用控制器名称ServiceController,你可以用你的控制器名称替换
Route::get('get-datatable', 'ServiceController@getDatatable')
在 js 中:
$(document).ready(function(){
$(function() {
var baseurl = window.location.protocol + "//" + window.location.host;
var table = $('#table').DataTable({
processing: true,
serverSide: true,
ajax: baseurl + "/get-datatable",
columns: [
{ data: 'id', name: 'id' },
{ data: 'name', name: 'name' },
{ data: 'price', name: 'price' },
{ data: 'amount', name: 'amount' },
{ data: 'share', name: 'share' }
]
});
});
});
在 blade 文件中:
<table class="table table-bordered" id="table">
<thead>
<tr>
<th>Id</th>
<th>Name</th>
<th>Price</th>
<th>Amount</th>
<th>Share</th>
</tr>
</thead>
</table>
我对 Yajra\DataTables 有疑问; 当我尝试通过以下方式从控制器获取数据时:
$servives = DB::table('services')->where('status', '=', 3)->get();
return datatables($servives)->toJson();
它给我以下错误:
{"draw":1,"recordsTotal":3,"recordsFiltered":0,"data":[],"error":"Exception Message:\n\nUndefined index: data"}
在这里你可以查看我的js代码:
$(document).ready(function () {
$('#table').DataTable({
processing: true,
serverSide: true,
ajax: "{{ route('all.my_services') }}",
columns: [
{ data: 'id', name: 'id' },
{ data: 'name', name: 'name' },
{ data: 'price', name: 'price' },
{ data: 'amount', name: 'amount' },
{"mData": {},
"mRender": function (data, type, row) {
return '<a href="/partner-share-services?id='+ data.id + '"><button class="btn btn-success">Share</button></a>';
}
}
]
});
});
在这里你可以看到我的 $services 数组:
Collection {#326
#items: array:2 [
0 => {#319
+"id": 103
+"partner_id": 1004
+"name": "AI-92"
+"price": 146
+"amount": 9007
+"created_at": "2019-05-15 07:04:07"
+"updated_at": "2019-05-16 06:10:13"
+"is_active": null
+"status": 3
}
1 => {#332
+"id": 104
+"partner_id": 1004
+"name": "AI 95"
+"price": 190
+"amount": 650
+"created_at": "2019-05-16 06:49:19"
+"updated_at": "2019-05-16 06:52:34"
+"is_active": null
+"status": 3
}
]
}
怎么了?
在控制器中:
use DataTables;
use DB;
public function getDatatable(){
$services = DB::table('services')->where('status',3);
return Datatables::of($services)
->addColumn('share', function($services){
return '<a href="/partner-share-services?id='. $services->id . '"><button class="btn btn-success">Share</button></a>';
})
->rawColumns(['share'])
->make(true);
}
在路由文件中(web.php):
这里我使用控制器名称ServiceController,你可以用你的控制器名称替换
Route::get('get-datatable', 'ServiceController@getDatatable')
在 js 中:
$(document).ready(function(){
$(function() {
var baseurl = window.location.protocol + "//" + window.location.host;
var table = $('#table').DataTable({
processing: true,
serverSide: true,
ajax: baseurl + "/get-datatable",
columns: [
{ data: 'id', name: 'id' },
{ data: 'name', name: 'name' },
{ data: 'price', name: 'price' },
{ data: 'amount', name: 'amount' },
{ data: 'share', name: 'share' }
]
});
});
});
在 blade 文件中:
<table class="table table-bordered" id="table">
<thead>
<tr>
<th>Id</th>
<th>Name</th>
<th>Price</th>
<th>Amount</th>
<th>Share</th>
</tr>
</thead>
</table>