phase 6
This commit is contained in:
@@ -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();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user