This commit is contained in:
Doar Mihai
2026-04-03 20:33:24 +03:00
parent 1cbe5bfe3b
commit 19d5e23dfd
11 changed files with 2996 additions and 11 deletions
+15 -11
View File
@@ -124,18 +124,22 @@ class JobRepository
*/
public function getFeaturedJobs(int $limit = 10): Collection
{
return Job::with(['source', 'category', 'jobLocation'])
->active()
->select('jobs.*')
->selectRaw('ROW_NUMBER() OVER (PARTITION BY job_source_id ORDER BY posted_at DESC) as rn')
->fromSub(function ($query) {
$query->from('jobs')
->whereNull('deleted_at')
->whereNull('expires_at')
// First, build a subquery with window function
$subQuery = \DB::table('jobs')
->selectRaw('jobs.*, ROW_NUMBER() OVER (PARTITION BY job_source_id ORDER BY posted_at DESC) as rn')
->whereNull('deleted_at')
->where(function ($query) {
$query->whereNull('expires_at')
->orWhere('expires_at', '>', now());
}, 'jobs')
->havingRaw('rn <= 2') // Max 2 jobs per source
->latest('posted_at')
});
// Then query from that subquery filtering by rn
// Use withoutGlobalScopes to prevent SoftDeletes from adding WHERE clause
return Job::withoutGlobalScopes()
->with(['source', 'category', 'jobLocation'])
->fromSub($subQuery, 'ranked_jobs')
->whereRaw('ranked_jobs.rn <= 2')
->orderBy('posted_at', 'desc')
->limit($limit)
->get();
}