Laravel Eloquent accessor

Laravel Eloquent accessor

arnoldjp57arnoldjp57 Posts: 10Questions: 7Answers: 0

I have a server side datatable that uses dot notation to access eager loaded referenced model attributes .... All 99.9%. BUT, it does not "see" an accessor defined in the model. Single attributes work.
This Accessor works on blade .... $patn->full_name ....

Models

class Visi extends Model {

public function visi_patn()
  {
    return $this->hasOne('App\Patn','id','patn');
  }
}
class Patn extends Model {

public function getFullNameAttribute()
  {
    return $this->name . ', ' . $this->init . ' ' . $this->patn_titl->desc;
  }

  public function patn_titl()
  {
      return $this->hasOne('App\Base','id','titl');
  }
}

Javascript

$('#visis-table_a').DataTable({
    "serverSide":     true,
    "deferRender":    true,
    "scroller":       true,
"ajax": { "type": "POST",
      "url": '{!! url('datatables_visis') !!}',
      "data": function (d) {
        d._token = '{{csrf_token()}}';
      }
    },
columns: [
 { data: 'visi_patn.full_name', name: 'visi_patn.full_name' },
]
});

Controller

return Datatables::of( Visi::with(['visi_patn']))->make(true);  

Answers

  • arnoldjp57arnoldjp57 Posts: 10Questions: 7Answers: 0

    This is the error I get in chrome .....

    DataTables warning: table id=visis-table_a - Requested unknown parameter 'visi_patn.full_name' for row 0
    
  • allanallan Posts: 61,853Questions: 1Answers: 10,134 Site admin

    Does the JSON returned from the server for each row look like this:

    {
      "visi_patn.full_name": ...,
    }
    

    If so you will need to escape the . as explained in the columns.data documentation.

    Allan

This discussion has been closed.