Search code examples
ajaxlaraveleager

Can't Display the data from AJAX Eager Relationship Laravel


enter image description hereHaving difficulty to show the data from attendees table in blade view but in the network in the browser, it shows. In blade, it only shows the main model which is Activity and the data from attendees only outputs object. I need to outputs the data from attendees
e.g. attendee.fname

I'm using yajra data tables: here my browser info

Here my ajax

$(document).ready(function(){
 $('#student_table').DataTable({
    "processing": true,
    "serverSide": true,
    "ajax": "{{ route('ajaxdata.getdata') }}",
    "columns":[
        { data: "ActivityID" },
        { data:"attendee",name:"attendee.fname" } //cant show this need help
        ]

 });

  //Heres my controller

  function getdata()
  {
    //  $students = Student::select('first_name', 'last_name');
    //  return Datatables::of($students)->make(true);

    //$qr = QR::select('ActivityID', 'AttendeesID');
    //  return Datatables::of($qr)->make(true);
    //  $att = Attendee::select('fname','lname')->get();

    $qr = QR::with('attendee')->get();
    return Datatables::of($qr)->make(true);


  }
});

Heres my relationship

 public function attendee()
      {
          return $this->hasMany('App\Attendee','user_id','AttendeesID');
      }

public function qr()
    {
        return $this->belongsTo('App/QR','AttendeesID','user_id');
    }

Solution

  • You access relationship data in Datatables like as { data:"attendee.fname",name:"attendee.fname" }