search by multiple values using checkbox in laravel

  checkbox, laravel, multiple-select, search

I have job_sector table in which sector_id and job_id fields are there. I just want to search job_id by the sectors which I have selected through checkbox. One may select multiple sectors.

My model :

public function scopeSelectedOptions($query, $input = [])
    if(!empty($input)) {
        if(array_key_exists('sector_id', $input)) {
            $query->whereHas('sector_id', function($q) use ($input) {
                return $q->whereIn('sector_id', $input['sector_id']);
    return $query;

Controller :

public function jobsFilter(Request $request)
    $jobs = JobSector::SelectedOptions(request()->all())->get();
    return view('front.pages.job.jobfilter')->with(['title'=>'Job Filter', 'jobs' => $jobs]);           

Form from where I am selecting multiple sectors :

<form action="{{ route('job.jobfilter') }}" method="GET" class="mb-4">
    @foreach(get_sectors() as $k=>$s)                     
      <input type="checkbox" name="input[]" value="{{ $k }}">{{ $k }}<br>
    <input type="submit" value="Search" />

Query showing the output :

@foreach($jobs as $c)
    {{ $c->job_id }} <br>

It shows me all the job_id in the table.

Please help me out,

Source: Laravel

Leave a Reply