V
V
Vladimir Borisyuk2019-03-26 11:48:17
Laravel
Vladimir Borisyuk, 2019-03-26 11:48:17

How to implement sorting using laravel and datatable?

Hello!
There are following tables:
Emails [id, email, user_id],
Users [id, user_name, branch_id],
Branches [id, branch_name],
Users is related to Emails as hasMany
Emails is related to Users as belongTo
Branches is related to Users as hasMany
Users is related to Branches as belongTo
The task is to implement a table with live search by fields: user_name, branch_name, email + sorting 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;
})->implode(',');
})->addColumn('branch', function(User $user){
return $user->branch->branch_name;
})->toJson();
return $data;
This code did not solve the problem..
How to implement it on the server side?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question