Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wrong id column value #885

Closed
shadoWalker89 opened this issue Dec 9, 2016 · 3 comments
Closed

Wrong id column value #885

shadoWalker89 opened this issue Dec 9, 2016 · 3 comments

Comments

@shadoWalker89
Copy link

shadoWalker89 commented Dec 9, 2016

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
@yajra
Copy link
Owner

yajra commented Dec 10, 2016

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

@shadoWalker89
Copy link
Author

Thanks, it's working fine now

@dhcmega
Copy link

dhcmega commented Jul 24, 2017

@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

@github-actions github-actions bot locked and limited conversation to collaborators Oct 15, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants