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 :)