laravel-datatables
laravel-datatables copied to clipboard
Column name updated automatically when filtering related column with same column name
Summary of problem
I have a User datatable where I also display the Role name assigned to the user, for ACL I use Laratrust.
key thing to note is user name and role name both are displayed in the table ans we have column filter setup on the same.
The problem is when I filter roles name column all names in my user.name column gets updated to filtered role name.
Code snippet of problem
Following code is from class UsersDataTable extends DataTable {
Note that I use eager loading ->with('roles') on related roles table.
public function query(User $model)
{
$columns = [
'users.id', 'users.name', 'users.email', 'users.verified', 'users.active', 'users.created_at',
'users.updated_at',
];
$scope = $model->newQuery()->with('roles')->select($columns);
if(!Laratrust::hasRole('superadmin')){
return $scope->where('current_team_id', Auth::user()->currentTeam->id);
}
return $scope;
}
and to show the roles name column
->editColumn('roles.name', function ($user) {
return $user->roles->pluck('name')->implode(', ');
})
System details
- Operating System Ubuntu 16.04
- PHP Version 1.7
- Laravel Version 5.6
- Laravel-Datatables Version 8.7
Screenshots

After applying filter see the Name column

I added a filter on my roles.name column as following, and it seems to work now. Is there any better solution than this?
->filterColumn('roles.name', function ($query, $keyword) {
$query->whereHas('roles', function ($query) use ($keyword) {
$query->where('name',$keyword);
});
})
I am experiencing the exact same problem. My master table books has a title column. The related table categories also has a column called title. As soon as I click to order the category column, all columns with the same name in the books table get overriden (even id).
I set up a new project with fresh dependencies.
$('#table').DataTable({
serverSide: true,
processing: true,
ajax: url('books/data'),
stateSave: true,
columns: [
{
data: 'id',
name: 'id'
},
{
data: 'title',
name: 'title'
},
{
data: 'original_title',
name: 'original_title'
},
{
data: 'author.name',
name: 'author.name'
},
{
data: 'origin.title',
name: 'origin.title'
},
{
data: 'category.title',
name: 'category.title'
}
]
});
Update: its fixed if I do this:
$with = [
'author',
'origin',
'category'
];
return datatables()->eloquent(Book::select([
'books.*',
])->with($with))->toJson();
https://github.com/yajra/laravel-datatables/issues/1231
This issue is stale because it has been open for 30 days with no activity. Remove stale label or comment or this will be closed in 7 days.
This issue was closed because it has been inactive for 7 days since being marked as stale.