lundi 7 septembre 2020

search by multiple values using checkbox in laravel

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="" method="GET" class="mb-4">
                        
    @foreach(get_sectors() as $k=>$s)                     
      <input type="checkbox" name="input[]" value=""><br>
    @endforeach
    <input type="submit" value="Search" />
</form>

Query showing the output :

@foreach($jobs as $c)
     <br>
  @endforeach

It shows me all the job_id in the table.

Please help me out,




Aucun commentaire:

Enregistrer un commentaire