我将如何重构下面的代码,以便只有一个拒绝函数而不是两个,并且只有一个对数据库的调用而不是三个。我也试图没有任何重复。
$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);