How to implement sorting using laravel and datatable?

There are the following tables:

Emails [id, email, user_id],
Users [id, user_name, branch_id],
Branches [id, branch_name],

Users associated with Emails as hasMany
Emails associated with Users as belongTo

Branches tied to Users as hasMany
Users tied to Branches as belongTo

Task is to implement a table with the live search in the fields: user_name, branch_name, email + sort using Jquery datatable.

Code :
$model = User::with('emails')->with('branch');
$DataTables = new DataTables();
$data = $DataTables->eloquent($model)->addColumn('emails', function(User $user {
return $user->emails->map(function($email){
return $email->email;
})->addColumn('branch', function(User $user){
return $user->branch->branch_name;
return $data;

This code has not solved the problem..

How to implement it on the server side?
March 19th 20 at 08:37
0 answer

Find more questions by tags Laravel