yajra / laravel-datatables

jQuery DataTables API for Laravel
https://yajrabox.com/docs/laravel-datatables
MIT License
4.75k stars 859 forks source link

Ordering column that's a relation, changes id in action links #521

Closed nWidart closed 8 years ago

nWidart commented 8 years ago

Hello,

Summary of problem or feature request

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

Expected behaviour

When ordering on the company.name column, the $id should still be the id of the main data (user id in this case)

System details

Have I missed some option/config somewhere so that it keep using the correct $id on the row ?

Thank you!

yajra commented 8 years ago

@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.*');
nWidart commented 8 years ago

Hello,

Thank you, adding ->select('users.*') did indeed fix the swapping of ID issue. (although not fixing #522)

rizaldywirawan commented 5 years ago

2 app developers that i'd love chat in this issue >.<

3s777 commented 4 years ago

// 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

yajra commented 4 years ago

@3s777 thank you for the feedback. Docs can be updated on this repo. Can you please send a PR?