I'm trying to sorting the column named agency_name. But its not working. The full_name is perfectly working while the agency_name column is not working. So basically I trying to access the data which in the user_work_experiences table, so the way I access them is through user table and get current user work experiences. Im using yajra/datatables package. So here my table structure looks: -
Student Hostels
- user_id
Users:
- id
User Work Experiences
- user_id
- agency_name
Here the example code:
Controller:
public function getHostels()
{
$hostels = StudentHostel::with('user', 'user.userProfile', 'user.currentWorkExperience', 'status')->limit(100)->select('student_hostels.*');
return Datatables::of($hostels)
->addIndexColumn()
->editColumn('user.full_name', function (StudentHostel $hostel) {
return $hostel->user->full_name;
})
->editColumn('user.userProfile.address_1', function(StudentHostel $hostel) {
return $hostel->user->userProfile ? $hostel->user->userProfile->address_1 . " " . $hostel->user->userProfile->address_2 : null;
})
->addColumn('action', 'backEnd.hostel.datatables_action')
->orderColumn('user.full_name', function ($query, $order) {
$query->whereHas('user', function ($q) use($order) {
$q->orderBy('full_name', $order);
});
})
->orderColumn('user.current_work_experience.agency_name', function ($query, $order) {
$query->whereHas('user', function ($q) use ($order) {
$q->whereHas('currentWorkExperience', function ($q) use ($order) {
$q->orderBy('agency_name', $order);
});
});
})
->orderByNullsLast()
->make(true);
}
Views:
$('#hostel_datatable').DataTable({
processing: true,
serverSide: true,
ajax: "{{ route('hostel.application.list') }}",
columns: [
{data: 'DT_RowIndex', name: 'DT_RowIndex', orderable: false, searchable: false},
{data: 'user.full_name', name: 'user.full_name'},
{data: 'user.current_work_experience.agency_name', name: 'user.current_work_experience.agency_name'},
{data: 'user.userProfile.address_1', name: 'user.userProfile.address_1', orderable: false, searchable: false},
{data: 'status.base_setup_name', name: 'status.base_setup_name', orderable: false, searchable: false},
{
data: 'action',
name: 'action',
orderable: true,
searchable: true
},
]
});
I hope you guys can help me out. Thank you so much :)