jeudi 21 juin 2018

Populating Rows Using Multi-Leveled json in Laravel-dataTables

I am not able to find the correct object item from the json in the ajax return. Maybe someone could help.

Here is the code in my controller:

 public function getDatatable()
{
    $sessions =Session::with(['placement', 'placement.tutor', 'placement.students', 'placement.subject', 'placement.sessions'])
                ->where([
                    ['adm_billed_flag', '=', 'No'],
                    ['billed_flag', '=', 'No'],
                    ['cancelled', '=', 'No']
                ])->orderBy('created_at', 'desc');

    return Datatables::of($sessions)
                ->addColumn('actions', 'partials.links.sessions.sessions_all_actions')
                ->make(true);
}

This returns (example):

{
  "draw": 0,
  "recordsTotal": 40,
  "recordsFiltered": 40,
  "data": [
    {
      "id": "51952",
      "placement_id": "4043",
      "time_start": "5-15-2018 7:15 PM",
      "time_end": "5-15-2018 9:00 PM",
      "comments": "Omitted and I worked on review questions in preparation for her exam on Wednesday.",
      "billed_flag": "No",
      "adm_billed_flag": "No",
      "adm_invoice": "",
      "rate_student": "60.00",
      "rate_client": "200.00",
      "rate_tutor": "60.00",
      "qb_added": "No",
      "cancelled": "No",
      "created_at": "5-16-2018 10:02 AM",
      "updated_at": "2018-06-13 14:33:23",
      "placement": {
        "id": "4043",
        "student_id": "1121",
        "tutor_id": "241",
        "subject_id": "10",
        "grade_start": "",
        "grade_end": "",
        "active": "y",
        "client_rate": "200.00",
        "tutor_rate": "60.00",
        "created_at": null,
        "updated_at": "2018-05-16 16:12:43",
        "tutor": {
          "id": "241",
          "first_name": "Omitted",
          "last_name": "Omitted",
          "name": "",
          "email": "Omitted",
          "bio": "",
          "gender": "",
          "avatar": "default_avatar.png",
          "country": "US",
          "user_state": "Omitted",
          "city": "Omitted",
          "address": "Omitted",
          "address_2": "",
          "postal": "Omitted",
          "permissions": "",
          "active": "Yes",
          "intemail": "n",
          "provider": "",
          "provider_id": "",
          "last_login": null,
          "created_at": null,
          "updated_at": null,
          "deleted_at": null
        },
        "students": [
          {
            "id": "1121",
            "parent_id": "1803",
            "first_name": "Omitted",
            "last_name": "Omitted",
            "address": "Omitted",
            "address_2": "",
            "address_3": "",
            "city": "Omitted",
            "state": "Omitted",
            "zip": "Omitted",
            "country": "US",
            "zone": "2",
            "phone_1": "",
            "phone_2": "",
            "email": "",
            "school": "Omitted",
            "grade": "10",
            "dob": null,
            "avatar": "default_avatar.png",
            "comments_initial": "",
            "rate": 0,
            "active": "Yes",
            "created_at": "2018-05-16 09:12:04",
            "updated_at": null,
            "pivot": {
              "placement_id": "4043",
              "student_id": "1121"
            }
          }
        ],
        "subject": {
          "id": "10",
          "name": "Chemistry",
          "description": "",
          "active": "y",
          "created_at": "2004-10-05 13:32:26",
          "updated_at": "2004-10-05 20:32:36"
        },
        "sessions": [
          {
            "id": "51952",
            "placement_id": "4043",
            "time_start": "5-15-2018 7:15 PM",
            "time_end": "5-15-2018 9:00 PM",
            "comments": "Omitted and I worked on review questions in preparation for her exam on Wednesday.",
            "billed_flag": "No",
            "adm_billed_flag": "No",
            "adm_invoice": "",
            "rate_student": "60.00",
            "rate_client": "200.00",
            "rate_tutor": "60.00",
            "qb_added": "No",
            "cancelled": "No",
            "created_at": "5-16-2018 10:02 AM",
            "updated_at": "2018-06-13 14:33:23"
          }
        ]
      },
      "actions": "Test"
    },

The JavaScript code in my Blade template is:

<script>
$(document).ready(function(){
 $(function () {
    //Initialize Select2 Elements
    $('.select2').select2();


    $('#active-sessions-table').DataTable({
        processing: true,
        serverSide: true,
        ajax: '',
        "columns": [
            {
                data: 'actions', 
                name: 'actions', 
                orderable: false, 
                searchable: false, 
                visible: true
            },
            {
                data: 'time_start', 
                name: 'time_start', 
                orderable: true, 
                searchable: true, 
                visible: true
            },
            {
                data: 'first_name', 
                name: 'placement.students.first_name', 
                orderable: true, 
                searchable: true, 
                visible: true
            }
        ]
    });
  });
});
</script>

I have tried various versions base on what I could find on my research, but as of yet, haven't been able to find a solution.

This either returns a dataTables warning, [object Object] or nothing.

 data: 'first_name', 
 name: 'placement.students.first_name', 

Maybe, someone has dealt with this before, and provide a solution.

Thanks in advance.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire