Laravel-datatables: Wrong id column value

Created on 9 Dec 2016  ·  3Comments  ·  Source: yajra/laravel-datatables

Summary of problem or feature request

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

System details

  • Operating System
  • PHP Version : 5.6
  • Laravel Version 5.3.26
  • Laravel-Datatables 6.22.5
documentation question

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.

$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);

All 3 comments

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

Was this page helpful?
0 / 5 - 0 ratings

Related issues

macnux picture macnux  ·  3Comments

techguydev picture techguydev  ·  3Comments

ghost picture ghost  ·  3Comments

NidhiDesai11 picture NidhiDesai11  ·  3Comments

vipin733 picture vipin733  ·  3Comments