Tuesday, 8 October 2019

how to sort by a custom appended relation to model

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,
            "city_id": 1,
            "accommodation_status_id": 1,
            "child_age": null,
            "infant_age": null,
            "accommodation_provider_id": null,
            "grade_stars": 4,
            "accommodation_type_id": 1,
            "start_price": 4120000,
            "start_discount": 0,
            "name": "",
            "english_name": "Grand Hotel Ferdowsi",
            "description": "",
            "english_description": null,
            "address": ".",
            "english_address": null,
            "rules": null,
            "english_rules": null,
            "supply_team_note": null,
            "phone": null,
            "email": null,
            "cellphone": null,
            "receptionist": null,
            "is_removed": 0,
            "is_recommended": 1,
            "latitude": "35.68829700",
            "longitude": "51.4196500",
            "is_deleted": 0,
            "checkin_time": "14",
            "checkout_time": "12",
            "st_hid": 10,
            "created_at": "2019-05-22 19:20:02",
            "updated_at": "2019-07-31 18:50:53",
            "hits": 4,
            "cheapest_room": {
                "id": 1,
                "operator_id": 1,
                "accommodation_id": 3,
                "bed_count": null,
                "extra_bed_count": 1,
                "childs_count": 2,
                "room_type_id": null,
                "room_area": null,
                "has_breakfast": 1,
                "room_status_id": null,
                "room_image": null,
                "name": null,
                "english_name": null,
                "description": null,
                "english_description": null,
                "is_removed": 0,
                "is_deleted": 0,
                "st_hid": 10,
                "st_rid": 499,
                "created_at": "2019-05-25 13:30:00",
                "updated_at": "2019-06-30 17:05:18",
                "accommodation_room_id": 1,
                "net_price": null,
                 // this is the field i want to sort by
                "sales_price": 100,
                "extra_bed_price": 5170000,
                "half_charge_price": 1000,
                "half_board_price": 1000,
                "full_board_price": 1000,
                "foreign_net_price": 1000,
                "foreign_sales_price": 1000,
                "foreign_extra_bed_price": 1000,
                "foreign_half_charge_price": 1000,
                "foreign_half_board_price": 1000,
                "foreign_full_board_price": 1000,
                "commission_percent": 2,
                "foreign_commission_percent": 1,
                "discount_percent": 2,
                "foreign_discount_percent": 2,
                "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)
            ->allowedIncludes(['gallery','city','accommodationRooms','accommodationRooms.roomPricingHistorySearch'])
            ->allowedFilters([
//                'name', 'grade_stars','src',
//                AllowedFilter::custom('gallery', new FilterBedCount),
            AllowedFilter::scope('bed_count'),
            AllowedFilter::scope('filter_price'),
            AllowedFilter::exact('city_id'),
            AllowedFilter::exact('is_recommended'),
            AllowedFilter::exact('accommodation_type_id'),
            'name',
            ])
            ->allowedSorts([AllowedSort::custom('hits', new PriceSort(),'hits'),
//                'sales_price'
                ])
            ->paginate(10);
        return $data;
    }

in short form i want to sort my api based on sales price



from how to sort by a custom appended relation to model

No comments:

Post a Comment