$subDistricts = SubDistrict::where('dist_id',17)->where('status',1)->get();
foreach ($subDistricts as $subDistrict){
$subDistrict->totalproperties = Property::where('status',1)->where('sub_district_id',$subDistrict->id)->count();
}
$sorted = $subDistricts->sortBy([
['totalproperties', 'desc'],
]);
$sorted->values()->all();
return $sorted;
result is :
[
{
"id": 10,
"bn_name": "ঢাকা",
"name": "Dhaka",
"dist_id": 17,
"status": 1,
"creator": 1,
"created_at": "2021-05-09T02:42:15.000000Z",
"updated_at": "2021-05-27T12:11:58.000000Z",
"totalproperties": 1
},
{
"id": 1,
"bn_name": "2",
"name": "Kalabagan",
"dist_id": 17,
"status": 1,
"creator": 1,
"created_at": "2021-04-11T03:34:30.000000Z",
"updated_at": "2021-05-11T15:25:02.000000Z",
"totalproperties": 29
},
{
"id": 2,
"bn_name": "2",
"name": "Kafrul",
"dist_id": 17,
"status": 1,
"creator": 1,
"created_at": "2021-04-11T03:34:50.000000Z",
"updated_at": "2021-05-11T15:25:34.000000Z",
"totalproperties": 40
}
]