I have two tables tasks
and task_hours
, And I’m using one to many relationship in task model.(task can have many hours logs). And displaying task data at frontend in table (datatable) using with
relationships to task_hours
(as per query below). I need to sort by tasks
table’s columns (task_name
, created_at
) also I need to sort by total_hours
which is in task_hours
table, I’m using jQuery DataTable in front-end and getting column name and direction in request at my controller function.
This is my query that is only sorts for tasks table for now but, how can I tell if it is main table’s column to sort or relationship table’s column to sort ?
$obj1 = Task::with("task_hours");
$data = $obj1->orderBy($sort_col, $dir)->get()->toArray();
Let me know what is the best possible way to sort with laravel relationships? I want to do this while using relationships only and not with join query.
Thanks in advance 🙂
2
Answers
To solve this problem:
As you are using
with
the Task as Task instance with relation object task_hours.So, to deal with this situation instead of using
with
use join and addSelect:This shall fix your issue.
Since we cannot sort through
with
columns we are using normal sql joins and sorting it from sql queries.Full Solution
Not optimized but it will get you going since you’re using serverside true in datatables:
Since you are getting all the data anyway you can sort in PHP rather than the database:
This avoids the join that would will need to do if you want to order within the SQL query.