Hello,
I have a table of users, where the last column is a relation (company name). Meaning my getColumns array has one key as following:
'company_id' => 'company.name',
I also have a custom partial to add actions, in my ajax method:
->addColumn('action', 'user::admin.partials.installers_datatables_actions')
This is the content of the partial, nothing fancy, just view/edit/delete buttons:
<div class="btn-group">
<a href="{{ route('admin.user.installer.show', [$id]) }}" class="btn btn-default btn-flat"><i class="fa fa-eye"></i></a>
<a href="{{ route('admin.user.installer.edit', [$id]) }}" class="btn btn-default btn-flat"><i class="fa fa-pencil"></i></a>
<button class="btn btn-danger btn-flat" data-toggle="modal" data-target="#modal-delete-confirmation" data-action-target="{{ route('admin.user.installer.destroy', [$id]) }}"><i class="fa fa-trash"></i></button>
</div>
On display this works perfectly, no issues.
However, when filtering on the company name it changes the $id variable in the action partial to the id of a company _for that row_.
This obviously is an issue since the view/edit/delete buttons are no more pointing to the correct ID (user id).
When ordering on the company.name column, the $id should still be the id of the main data (user id in this case)
Have I missed some option/config somewhere so that it keep using the correct $id on the row ?
Thank you!
@nWidart, you need to specify the selected field on your main model when using relationships to avoid this issue. I also encountered this and thus I updated the demo and added a note like below:
// It is advised that you include select('table.*') on query to avoid weird issues where id from related model replaces the id of the main model.
$posts = Post::with('user')->select('posts.*');
Hello,
Thank you, adding ->select('users.*') did indeed fix the swapping of ID issue. (although not fixing #522)
2 app developers that i'd love chat in this issue >.<
// It is advised that you include select('table.') on query to avoid weird issues where id from related model replaces the id of the main model.
$posts = Post::with('user')->select('posts.');
I think this info must be in main documentation https://yajrabox.com/docs/laravel-datatables/master/relationships
Becose i spent 4 hours to find this topic
@3s777 thank you for the feedback. Docs can be updated on this repo. Can you please send a PR?
Most helpful comment
@nWidart, you need to specify the selected field on your main model when using relationships to avoid this issue. I also encountered this and thus I updated the demo and added a note like below: