What I’m trying to do is this:
I have two tables with One to Many relationship
sectors
id
name
position
set_plan_id
seat_plans
id
name
order
I want to sort all sectors based on the order field present in seat_plans (which is of type numeric, so sort in ascending order).
I tried this code but I get unsorted sectors.
$sectors= Sector::whereHas('seat_plan', function ($query) {
$query->orderBy('order');
})->get();
Can anyone kindly help me? thank you all
2
Answers
You have the option to do a join with the table like so:
Or make use of the aggregate method like shown below:
reference: https://laravel.com/api/9.x/Illuminate/Database/Eloquent/Concerns/QueriesRelationships.html#method_withAggregate
try this:
for more approaches take a look at this link.