i have a custom appended relation to model that i want to sort my parents according to that so this is the result of my api when i call it :
{
"id": 3,
"operator_id": 12,
"created_at": "2019-05-22 19:20:02",
"updated_at": "2019-07-31 18:50:53",
"hits": 4,
"cheapest_room": {
"id": 1,
"accommodation_id": 3,
"bed_count": 1,
"name": null,
"english_name": null,
"description": null,
"english_description": null,
"created_at": "2019-05-25 13:30:00",
"updated_at": "2019-06-30 17:05:18",
"accommodation_room_id": 1,
// this is the field i want to sort by
"sales_price": 100,
"from_date": "2019-05-25 00:00:00",
"to_date": "2019-08-30 23:59:59"
}
i am using spatie query builder for sorting and filtering this but i want to do a sort on sales_price on cheapest room object this is my code below :
public function scopeFilter(){
$data = QueryBuilder::for(Accommodation::class)
->allowedAppends(['cheapestroom'])
->allowedIncludes(['gallery','city','accommodationRooms','accommodationRooms.roomPricingHistorySearch','discounts'])
->allowedSorts([
AllowedSort::custom('discount', new DiscountSort() ,'amount'),
'discounts.amount'
])
->allowedFilters([
AllowedFilter::scope('bed_count'),
AllowedFilter::scope('filter_price'),
AllowedFilter::exact('grade_stars'),
AllowedFilter::exact('city_id'),
AllowedFilter::exact('is_recommended'),
AllowedFilter::exact('accommodation_type_id'),
'name',
])
->paginate(10);
return $data;
}
in short form i want to sort my api based on sales price