使用搜索表单中的一个参数进行搜索,该参数可以使用多个参数Laravel

时间:2017-02-04 12:50:15

标签: php laravel laravel-5.3 blade

我创建了一个搜索表单,可以有3个参数来搜索db并返回结果。表格中的所有参数或表格字段都是下拉列表。

表格代码:

{!!Form::open(['route' => 'search','class' => 'form-inline pull-left', 'id' => 'search-form', 'method' => 'GET', 'role' => 'search'])!!}
    <select class=" form-control" name="country">
       <option> Select Country</option>
       @foreach($countries as $country)
       <option value="{{$country->name}}">{{ucfirst($country->name)}}</option>
       @endforeach
    </select>
    <select class=" form-control" name="category">
       <option value="">Select Activity</option>
       @foreach($categories as $category)
       <option value="{{$category->name}}">{{ucfirst($category->name)}}</option>
       @endforeach
    </select>
    <select class=" form-control" name="days">
       <option value="">Select Duration (Days)</option>
       <option value="1|5">1-5</option>
       <option value="10|15">10-15</option>
       <option value="20|30">20-30</option>
       <option value="30|60">30-Above</option>
    </select>
 {{ Form::submit('Search', ['class' => 'btn btn-primary btn-lg','id' => 'search'] )}}

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

我在控制器中只使用一个参数进行搜索的方法:

    public function search(Request $request)
{
    $days = $request->days;
    $days_explode = explode('|', $days);

    if (isset($request->country) && !isset($request->category) && !isset($request->days)) {

        $query= Tour::whereHas('country', function($r) use($request) {
            $r->where('name','=', $request->country);
        })
        ->paginate(8);

        return view('public.tour.list')->withResults($query);
    }
    else{
    $query= Tour::whereHas('country', function($r) use($request) {
            $r->where('countries.name','=', $request->country);
        })
        ->whereHas('category', function($s) use($request) {
            $s->where('categories.name','=', $request->category);
        }) 
        ->whereHas('country', function($r) use($request) {
            $r->where('countries.name','=', $request->country);
        })
        ->whereBetween('days', [$days_explode[0], $days_explode[1]])   
        ->paginate(8);
    return view('public.tour.list')->withResults($query);
    }

我正在尝试使我的表格灵活。它应返回结果是否传递1,2或3参数。使用上面的代码,如果将所有3个参数传递给它,则表单返回结果。如果只传递一个参数,则会给出错误Undefined offset: 1

1 个答案:

答案 0 :(得分:1)

您可以先检查每个参数,然后决定是否将其添加到查询中。做这样的事情:

$query = Tour::where(function($q) use($request) {
    if (!empty($request->country)) {
        $q->whereHas('country', function($r) use($request) {
            $r->where('countries.name', $request->country);
        });
    }

    if (!empty($request->category)) {
        $q->whereHas('category', function($r) use($request) {
            $r->where('categories.name', $request->category);
        });
    }
})
->whereBetween('days', [$days_explode[0], $days_explode[1]])
->paginate(8);

或者你可以这样做:

$query = Tour::query();

if (!empty($request->country)) {
    $query = $query->whereHas('country', function($r) use($request) {
        $r->where('countries.name', $request->country);
    });
}

if (!empty($request->category)) {
    $query = $query->whereHas('category', function($s) use($request) {
        $s->where('categories.name', $request->category);
    });
}

$query = $query->whereBetween('days', [$days_explode[0], $days_explode[1]])
               ->paginate(8);