aidysproule's avatar

Query number range between two columns

I have a job posting website and I want users to be able to select a minimum salary and it to show all jobs where the selected salary falls within the jobs salary range. I cannot get it to work. For example, if a user selects a minimum salary of £30k, I want to display jobs what have a range of £25k-£35k, not just jobs with a salary range starting at £30k. The job salary range is in my DB as two columns, 'salary_from' and 'salary_to' Any ideas?

You can see a live example here, if you change the salary to £40k, you should still see 3 jobs: https://jobhuntni.com/jobs?salary=30000&search=vue

0 likes
1 reply
krisi_gjika's avatar

I think what you mean is:

$salary = 30000;
$threshold = 5000;

Job::query()
  ->where('salary_from', '>=', $salary - $threshold) // starting from 25
  ->where('salary_to', '<=', $salary + $threshold)    // ending on 35
  ->get();

Please or to participate in this conversation.