Description
i have below tables
users
id
name
last_name
categories
id
name
category_user
user_id
category_id
user can have many categories, i mean they have Many to Many relationship.
in UserController.php i have below code
public function GetUsersData(){
$users = User::with(['categories'])->select('users.*');
return Datatables::of($users)
->addColumn('name_of_user_category', function (User $user) {
return $user->categories->map(function($category) {
return $category->name;
})->implode('<br>');
})
->toJson();
}
and in users.balde.php i have below javascript code:
$('#users-table').DataTable({
processing: true,
serverSide: true,
ajax: '',
columns: [
{data: 'name', name: 'name'},
{data: 'last_name', name: 'last_name'},
{data: 'name_of_user_category', name: 'name_of_user_category.name'}
]
});
after first loading data table every thing works fine but when i wanna sort table by name_of_user_category column, the other column that contains name of user, it fills with name of category too, some things goes wrong here, because user and cateegory have a column with same name this problem will happen after sorting the column that contain name of category.
i had hard time to find a way to sort column for many to many relation ship column but now i have new problem.
i tried to share my problem as clear as i can, if you need more info please let me know.
thanks in advance
System details
- Operating System : Ubuntu
- PHP Version:7.2
- Laravel Version: 6.2
- Laravel-Datatables Version:"^9.10"