I just wanna ask if my approach is good? I have a api respopnse which a merchants where i want to do is that if merchants with the same group_id appeared more than once i will put it on a group of merchants then exclude all the merchants that part in a group in a merchants that appeared once only. I do here is that get the group_ids then loop through there and after fetching exclude the group_ids appeared more than once[enter image description here][1]
$merchantsHaveGroupId = $merchants->whereNotNull('group_id')->get();
$groupIds = $merchantsHaveGroupId->pluck('group_id')->toArray();
$groupIds = array_count_values($groupIds);
$numOfMerchantsToBeOnParent = 1;
$listOfMerchantWillBeExcluded = [];
$parentData = [];
foreach($groupIds as $key => $value){
if($value > $numOfMerchantsToBeOnParent){
$merchantGroupParent = MerchantGroup::find($key);
$children = $merchantGroupParent->merchants()->whereIn('id',$ids);
$childrenIds = $children->pluck('id')->toArray();
$constructData = array(
"id" => $key,
"title" => $merchantGroupParent->title,
"isParent" => true,
"children_count" => $groupIds[$key],
"children_merchants" => $children->get()->toArray(),
"childrens_ids" => $childrenIds
);
array_push($parentData, $constructData);
array_push($listOfMerchantWillBeExcluded, $key);
}
}
// now exclude the found ids now part of a group
$merchants = $merchants->whereNotIn('group_id',$listOfMerchantWillBeExcluded);
$merchants = Merchant::sortRestaurants($merchants, $favorites, $cityautoassign);
// for pagination
$merchants = $merchants->toArray();
// merge merchants
if(!empty($parentData)){
$merchants = array_merge($parentData,$merchants);
}
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire