I am trying to get a new column "Use Case" using DataTables, but when I trying to get value tc_scenario from database and return this value to Html using $data->tc_scenario
I got an error like this :
Undefined variable: data
public function dtindex(Request $request)
{
$tanggal = $request->input('tanggal');
$selectorx= $request->input('selector1');
$selectory= $request->input('selector2');
$selectorz= $request->input('selector3');
$data = DB::select(DB::raw("
select DISTINCT id, tc_scenario from tbl_master
"));
$data= collect($data);
return Datatables::of($data)
->addColumn('tc_scenario', function ($data) use ($selectorx, $selectory, $selectorz){
$selector1 = $selectorx;
$selector2 = $selectory;
$selector3 = $selectorz;
return value($data, compact('selector1', 'selector2', 'selector3'));
})
->make(true);
}
Scripts :
$(function () {
$('#dtindex').DataTable({
processing: true,
serverSide: true,
ajax: {
url: '{!! route('dtindex') !!}',
type: 'POST',
data: function (d) {
d.selector1 = '{{$selector1}}';
d.selector2 = '{{$selector2}}';
d.selector3 = '{{$selector3}}';
d._token = $('meta[name="csrf-token"]').attr('content');
}
},
columns: [
{
render: function (data, type, row, meta) {
return meta.row + meta.settings._iDisplayStart + 1;
}
},
{
data: 'tc_scenario', name: 'tc_scenario',
render: function (data, type, row, meta) {
return '<form name="detailcust" method="post" action="{!! route('report') !!}">',
'<input name="_token" type="hidden" value="csrf_token()"/>',
'<input name="inputan" id="inputan" type="hidden" value="{{$selector1}}"/>' ,
'<input name="inputan" id="inputan" type="hidden" value="{{$selector2}}"/>',
'<input name="inputan" id="inputan" type="hidden" value="{{$selector3}}"/>',
'<input class="btn btn-link btn-xs" type="submit" name="submit" value="{{$data->tc_scenario}}" id="submit"><h4 style="visibility:hidden">{{$data->tc_scenario}}</h4></input></form>';
}
}
]
});
});
Html :
<div class="row">
<div class="col-md-pull-12 table-responsive">
<table class="table table-bordered table-striped"
style="font-size: 10px; text-align: center;"
id="dtindex">
<thead>
<tr>
<th style="text-align: center">No.</th>
<th style="text-align: center">Use Case</th>
</tr>
</thead>
</table>
</div>
</div>
I expect to be able to get value from tc_scenario, and this is my table in database