How would I refactor the code below so that there is only one reject function not two and have only one call to the db instead of three. I am also trying to not have any duplicates.
$latestListings = $repo->whereExclusive(['property_status' => 'Active'],['created_at' => 'desc'],[],0, 4);
$latestListingsIds = $latestListings->map(function (Listing $listing) {
return $listing->id;
})->toArray();
$highCflListings = $repo->whereExclusive(['property_status' => 'Active'],['cfl' => 'desc'],[],0, 4);
$highCflListingIds = $highCflListings->map(function (Listing $listing) {
return $listing->id;
})->toArray();
$highCflListingsOccupied = $repo->whereExclusive(
['property_status' => 'Active', 'occupied_percentage' => 100],
['cfl' => 'desc'],
[],
0,
12
)->reject(function (Listing $listing) use ($latestListingsIds) {
return in_array($listing->id, $latestListingsIds);
})->reject(function (Listing $listing) use ($highCflListingIds) {
return in_array($listing->id, $highCflListingIds);
})->take(4);