@jlrdw But I need to shift the projects as promotion on the top not on its origin place for Example when projects are displayed, two are displayed as a regular and the third is a promoter important I want to handle that with the query and this is my code to getting the point
$queryProjects = Project::
->selectRaw($selectRaw)
->where([['verified', true], ['active', true]])
->orderBy('id', 'DESC');
$allProjects = $queryProjects->get();
$promotedProjectsCollection = collect($queryProjects->promoted()->get());
$promotedProjectsCollection = $promotedProjectsCollection->keyBy('id');
$isAllPromoted = $allProjects->every(function ($project) use ($promotedProjectsCollection) {
return $promotedProjectsCollection->contains('id', $project->id);
});
if ($isAllPromoted && ! $allProjects->isEmpty()) {
$allProjectsWithPromoted = $allProjects;
} else {
$filteredProjects = $allProjects->reject(function ($project) use ($promotedProjectsCollection) {
return $promotedProjectsCollection->contains('id', $project->id);
});
$i = 0;
$allProjectsWithPromoted = [];
foreach ($filteredProjects as $project) {
$allProjectsWithPromoted[] = $project;
$i++;
if ($i % 2 == 0 && ! $promotedProjectsCollection->isEmpty()) {
$allProjectsWithPromoted[] = $promotedProjectsCollection->shift();
}
}
}
$collection = collect($allProjectsWithPromoted);
$perPage = 20;
$currentPage = request()->input('page', 1);
$paginatedItems = $collection->slice(($currentPage - 1) * $perPage, $perPage)->values();
$paginator = new \Illuminate\Pagination\LengthAwarePaginator(
$paginatedItems,
$collection->count(),
$perPage,
$currentPage,
[
'path' => request()->url(),
'query' => request()->query(),
]
);