laravel-datatables icon indicating copy to clipboard operation
laravel-datatables copied to clipboard

Column name updated automatically when filtering related column with same column name

Open karmendra opened this issue 7 years ago • 4 comments

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

image

After applying filter see the Name column image

karmendra avatar Jul 09 '18 10:07 karmendra

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

karmendra avatar Jul 10 '18 07:07 karmendra

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'
                    }
                ]
            });

pr4xx avatar Nov 27 '18 22:11 pr4xx

Update: its fixed if I do this:

$with = [
            'author',
            'origin',
            'category'
        ];
return datatables()->eloquent(Book::select([
            'books.*',
        ])->with($with))->toJson();

pr4xx avatar Nov 28 '18 14:11 pr4xx

https://github.com/yajra/laravel-datatables/issues/1231

saskysamonte avatar Nov 29 '21 06:11 saskysamonte

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.

github-actions[bot] avatar Aug 17 '23 00:08 github-actions[bot]

This issue was closed because it has been inactive for 7 days since being marked as stale.

github-actions[bot] avatar Aug 24 '23 00:08 github-actions[bot]