Ad
"Undefined Index: Data"
I have problem with Yajra\DataTables; When I am trying to get data from controller via:
$servives = DB::table('services')->where('status', '=', 3)->get();
return datatables($servives)->toJson();
It gives me the following error:
{"draw":1,"recordsTotal":3,"recordsFiltered":0,"data":[],"error":"Exception Message:\n\nUndefined index: data"}
Here you can check my js code:
$(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 target="_blank" rel="nofollow noreferrer" href="/partner-share-services?id='+ data.id + '"><button class="btn btn-success">Share</button></a>';
}
}
]
});
});
Here you can see my $services array:
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
}
]
}
What is wrong?
Ad
Answer
In controller:
use DataTables;
use DB;
public function getDatatable(){
$services = DB::table('services')->where('status',3);
return Datatables::of($services)
->addColumn('share', function($services){
return '<a target="_blank" rel="nofollow noreferrer" href="/partner-share-services?id='. $services->id . '"><button class="btn btn-success">Share</button></a>';
})
->rawColumns(['share'])
->make(true);
}
In route file (web.php):
Here I use controller name ServiceController you can replace with your controller name
Route::get('get-datatable', '[email protected]')
In 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' }
]
});
});
});
In blade file:
<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>
Ad
source: stackoverflow.com
Related Questions
- → OctoberCMS - How to make collapsible list default to active only on non-mobile
- → How to update data attribute on Ajax complete
- → October CMS - Radio Button Ajax Click Twice in a Row Causes Content to disappear
- → Octobercms Component Unique id (Twig & Javascript)
- → when i send data with ajax(jquery) i don't find the values in controller without form in laravel 5.1
- → DropzoneJS & Laravel - Output form validation errors
- → Knockout JS - How to return empty strings for observable fields
- → How to replace *( in a string
- → Get the calling element with vue.js
- → Sent Variable to Controller via Ajax in Blade
- → AJAX folder path issue
- → Onclick with argument causes javascript syntax error
- → KNockout JS - Automatic reload of ajax call
Ad