Values of the id
column are wrong when the id
isn't requested from the javascript side.
The problem only appears only when i don't mention the id
column in the dataTables setting columns
.
$('#table').DataTable({
columns: [
{ data: 'id', name: 'id' }, // ----> Problem appear when i don't mention this column in javascript
{ data: 'user.first_name', name: 'user.first_name' },
...
]
})
In the PHP side i have something like this
$students = Student::with(['user', ...]);
return Datatables::of($students)
->addColumn('actions', function ($row)
{
$row->id // ----> Will contain wrong value, it will contain users.id instead of students.id
return $actions;
})
->make(true);
The value of id
will be users.id
instead of students.id
I think this is a bug in the laravel framework and the work around to fix this is by adding select('students.*')
on your query as documented here.
$students = Student::with(['user', ...])->select('students.*');
return Datatables::of($students)
->addColumn('actions', function ($row)
{
$row->id // ----> Will contain wrong value, it will contain users.id instead of students.id
return $actions;
})
->make(true);
Thanks, it's working fine now
@yajra, your answer helped me, thanks. It would be better is you remove the comment as in your example it should be the opposite. It will contain the correct value.
Thanks
Most helpful comment
I think this is a bug in the laravel framework and the work around to fix this is by adding
select('students.*')
on your query as documented here.