5 months ago

Fetch model attributes in Yajra datatable

Posted 5 months ago by kshitizmittal

I need to fetch custom model attribute in Yajra datatable, also there are custom attributes in relationship also which I need to fetch in datatable.

I have one attendance_status custom attribute in AttendanceReport.

And one full_name custom attribute in Employeedetail model which I am fetching here in with() function.

This is controller code :-

public function serverSide(){
        $attendacereport = AttendanceReport::whereRaw("emp_id='710102'")->with('employeedetail.departments','employeedetail.designations')->->select(['emp_id','punch_in','punch_out','attendance_status'])get();

        return Datatables::of($attendacereport)->make();

Datatble Script :-

    $(document).ready(function() {
            processing: true,
            serverSide: true,
            pageLength: 100,
            ajax: '{{ url('serverSide') }}',
            initComplete:function( settings, json){
                // call your function here
            columns: [
                { data: 'emp_id', name: 'emp_id' },
                { data: 'employeedetail.full_name' , name: 'employeedetail.full_name' },
                { data: 'punch_in', name: 'punch_in' },
                { data: 'punch_out', name: 'punch_out' },
                { data: 'attendance_status', name: 'attendance_status' }

Struggling to get this please help me out.

Please sign in or create an account to participate in this conversation.