Laravel Eloquent accessor
Laravel Eloquent accessor
arnoldjp57
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);
This discussion has been closed.
Answers
This is the error I get in chrome .....
Does the JSON returned from the server for each row look like this:
If so you will need to escape the
.
as explained in thecolumns.data
documentation.Allan