This commit is contained in:
Doar Mihai
2026-04-03 00:39:57 +03:00
parent 4e6b636787
commit 2b930a0636
6 changed files with 868 additions and 0 deletions
+79
View File
@@ -108,4 +108,83 @@ class Job extends Model
{
return $query->where('experience_level', $level);
}
/**
* Scope a query to search by keywords in title, company, and description.
*/
public function scopeSearchKeywords($query, string $keywords)
{
return $query->where(function ($q) use ($keywords) {
$q->where('title', 'LIKE', "%{$keywords}%")
->orWhere('company', 'LIKE', "%{$keywords}%")
->orWhere('description', 'LIKE', "%{$keywords}%");
});
}
/**
* Scope a query to filter by category.
*/
public function scopeInCategory($query, int $categoryId)
{
return $query->where('job_category_id', $categoryId);
}
/**
* Scope a query to filter by location.
*/
public function scopeInLocation($query, string $location)
{
return $query->where(function ($q) use ($location) {
$q->where('location', 'LIKE', "%{$location}%")
->orWhereHas('jobLocation', function ($q2) use ($location) {
$q2->where('city', 'LIKE', "%{$location}%")
->orWhere('state', 'LIKE', "%{$location}%")
->orWhere('country', 'LIKE', "%{$location}%");
});
});
}
/**
* Scope a query to filter by salary range.
*/
public function scopeWithinSalaryRange($query, ?float $minSalary = null, ?float $maxSalary = null)
{
if ($minSalary) {
$query->where(function ($q) use ($minSalary) {
$q->where('salary_max', '>=', $minSalary)
->orWhereNull('salary_max');
});
}
if ($maxSalary) {
$query->where(function ($q) use ($maxSalary) {
$q->where('salary_min', '<=', $maxSalary)
->orWhereNull('salary_min');
});
}
return $query;
}
/**
* Scope a query to filter by posted date.
*/
public function scopePostedWithin($query, int $days)
{
return $query->where(function ($q) use ($days) {
$q->where('posted_at', '>=', now()->subDays($days))
->orWhere(function ($q2) use ($days) {
$q2->whereNull('posted_at')
->where('created_at', '>=', now()->subDays($days));
});
});
}
/**
* Scope a query to filter by company.
*/
public function scopeByCompany($query, string $company)
{
return $query->where('company', 'LIKE', "%{$company}%");
}
}