when the js responds, the data has already been ordered by example image response js :
example controller and query :
$query = Verifikasi_tte::join('surat_ttes', 'surat_ttes.id', '=', 'verifikasi_ttes.surat_tte_id')
->join('users', 'users.id', '=', 'surat_ttes.user_id')
->join('jenis_surat_ttes', 'jenis_surat_ttes.id', '=', 'surat_ttes.jenis_surat_tte_id')
->select('surat_ttes.id','surat_ttes.klasifikasi','surat_ttes.no_surat','surat_ttes.tgl_surat','surat_ttes.created_at',
'jenis_surat_ttes.nama_jenis_surat','users.name','verifikasi_ttes.surat_tte_id','verifikasi_ttes.status')
->orderBy('surat_ttes.id','DESC')->get();
return Datatables::of($query)
example view in ajax datables :
<script type="text/javascript">
$(function (e) {
$('#data-table').DataTable({
pageLength: 10,
processing: true,
serverSide: true,
stateSave: true,
ajax: "{{ url ('/verifikasi/data') }}",
columns: [
{data: 'name', name: 'name'},
{data: 'nama_jenis_surat', name: 'nama_jenis_surat'},
{data: 'no_surat', name: 'no_surat'},
{data: 'dates', name: 'dates'},
{data: 'status', name: 'status'},
{data: 'action', name: 'action', orderable: false, searchable: false}
]
});
});
</script>
is there something wrong with my controller?
CodePudding user response:
Try removing the get()
function in the controller.
$query = Verifikasi_tte::join('surat_ttes', 'surat_ttes.id', '=', 'verifikasi_ttes.surat_tte_id')
->join('users', 'users.id', '=', 'surat_ttes.user_id')
->join('jenis_surat_ttes', 'jenis_surat_ttes.id', '=', 'surat_ttes.jenis_surat_tte_id')
->select('surat_ttes.id','surat_ttes.klasifikasi','surat_ttes.no_surat','surat_ttes.tgl_surat','surat_ttes.created_at',
'jenis_surat_ttes.nama_jenis_surat','users.name','verifikasi_ttes.surat_tte_id','verifikasi_ttes.status')
->orderBy('surat_ttes.id','DESC')
return Datatables::of($query)
This happens because the Datatables plugin wants a eloquent query object to do the sorting and searching features.
CodePudding user response:
because I use soft delete, so if to use the query builder it might not be possible for soft delete but eloquent, now here I combine eloquent with query builder, and I have tried not to use get() it works but to search data in the datatables package yajra there was an error in the form of the column field from the join query was not found
if example not use get() :
$query = Verifikasi_tte::join('surat_ttes', 'surat_ttes.id', '=', 'verifikasi_ttes.surat_tte_id')
->join('users', 'users.id', '=', 'surat_ttes.user_id')
->join('jenis_surat_ttes', 'jenis_surat_ttes.id', '=', 'surat_ttes.jenis_surat_tte_id')
->select('surat_ttes.id','surat_ttes.klasifikasi','surat_ttes.no_surat','surat_ttes.tgl_surat','surat_ttes.created_at',
'jenis_surat_ttes.nama_jenis_surat','users.name','verifikasi_ttes.surat_tte_id','verifikasi_ttes.status')
->orderBy('surat_ttes.id','DESC')
return Datatables::of($query)
when I create a column in the form of a function, the return result of the query variable :
$query = Verifikasi_tte::join('surat_ttes', 'surat_ttes.id', '=', 'verifikasi_ttes.surat_tte_id')
->join('users', 'users.id', '=', 'surat_ttes.user_id')
->join('jenis_surat_ttes', 'jenis_surat_ttes.id', '=', 'surat_ttes.jenis_surat_tte_id')
->select('surat_ttes.id','surat_ttes.klasifikasi','surat_ttes.no_surat','surat_ttes.tgl_surat','surat_ttes.created_at',
'jenis_surat_ttes.nama_jenis_surat','users.name','verifikasi_ttes.surat_tte_id','verifikasi_ttes.status')
->orderBy('surat_ttes.id','DESC');
return Datatables::of($query)
->addColumn('dates', function ($query) {
$dates = Carbon::parse($query->created_at)->translatedFormat('l, d F Y');
return $dates;
})
->addColumn('name', function ($query) {
$name = $query->name;
return $name;
})
->addColumn('nama_jenis_surat', function ($query) {
$nama_surat = $query->nama_jenis_surat;
return $nama_surat;
})
->addColumn('no_surat', function ($query) {
$no_surat = $query->no_surat;
return $no_surat;
})
this method works and no error message appears in the column datatables, but what happens is that the search for query results in the datatables doesn't work .., is there anything to add to the ajax datatables yajra?, or change the controller in the query totally?