Laravel 5.4搜索多个复选框值的查询

时间:2017-02-07 12:59:03

标签: php laravel-5.4

我是复选框的新手。我想让用户根据复选框列表所代表的三个可能的过滤器进行搜索。     用户可以搜索不同的方式。         请帮我。             Here is my Html Code

{!! Form::open(['url' => 'search-job']) !!}
<p>
    Hours:
    <br/>
    <input type="checkbox" name="jobs_type[]" value="Part Time">Part Time
    <input type="checkbox" name="jobs_type[]" value="Full Time">Full Time
    <input type="checkbox" name="jobs_type[]" value="Temporary">Temporary
</p>

<p>
    category:
    <br/>

    @foreach($job_function as $job_category)
    <li>
        <div class="checkbox">
            <label>
                <input type="checkbox" name="job_function[]" value="{{ $job_category->id }}">{{$job_category->name}}
            </label>
        </div>
    </li>
    @endforeach

</p>

<p>
    salary
    <br/>

    <input type="checkbox" name="salary[]" value="1000"/> upto $1000<br/>
    <input type="checkbox" name="salary[]" value="1000-1999">$1,000- 2,000<br/>
    <input type="checkbox" name="salary[]" value="2000-3999">$2,000 - 4,000<br/>
</p>

<input type="submit" value="Search">

{!! Form::close() !!}

1 个答案:

答案 0 :(得分:0)

public function job_search(Request $request){


$builder = Job::query();
$term = Request::all();

if(!empty($term['job_function'])){
   $builder->whereIn('category_id',$term['job_function']);

}
if(!empty($term['jobs_type'])){
   $builder->orWhereIn('jobs_type',$term['jobs_type']);
}
if(!empty($term['salary'])){
    $builder->orWhereIn('salary','=',$term['salary']);
}

$result = $builder->orderBy('id')->get();

return view('frontend.pages.job.find_jobs')->withResult($result);
    }