Laravel-datatables: Ordering column that's a relation, changes id in action links

Created on 20 Apr 2016  路  5Comments  路  Source: yajra/laravel-datatables

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

  • Operating System: Ubuntu 14.04
  • PHP Version: 7.0.5
  • Laravel Version 5.1.34
  • Laravel-Datatables Version: 6.10.1

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

Thank you!

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:

// 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.*');

All 5 comments

@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?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

vipin733 picture vipin733  路  3Comments

t0n1zz picture t0n1zz  路  3Comments

FilipeBorges1993 picture FilipeBorges1993  路  3Comments

Mopster picture Mopster  路  3Comments

shadoWalker89 picture shadoWalker89  路  3Comments