具有多种条件的Laravel搜索查询

时间:2014-07-20 16:47:01

标签: php laravel-4

新手PHP / Laravel,请耐心等待。

我有一个网页,根据狗,品种,性别和半径的3个标准进行搜索。

这是相关代码:

搜索页面

<div class="col-md-12 zero-pad-left zero-pad-right"> 
{{ Form::open(array('action' => array('DogsController@index'), 'class'=>'form width88', 'role'=>'search', 'method' => 'GET')) }} 
<div id="prefetch">
  {{ Form::text('search-breed', null, array('class' => 'typeahead form-group form-control', 'placeholder' => 'Search by breed here...')) }}
  {{ Form::text('sex', null, array('class' => 'form-group form-control', 'placeholder' => 'Search by sex here...')) }}
  {{ Form::text('miles', null, array('class' => 'form-group form-control', 'placeholder' => 'Search by distance here...')) }}




</div>
{{ Form::submit('Search', array('class' => 'btn btn-default search-bar-btn')) }}
{{ Form::close() }} 

ControllerPage

class DogsController extends \BaseController {

public function __construct()
{
    // call base controller constructor
    parent::__construct();

    // run auth filter before all methods on this controller except index and show
    $this->beforeFilter('auth', array('except' => array('show')));
}

/**
 * Display a listing of the resource.
 *
 * @return Response
 */
public function index()
{
    if (Input::has('search')) {
        $queryString = Input::get('search');
        $dogs = Dog::where('name', 'LIKE', "%$queryString%")->orderBy('name')->paginate(5);
    }

    elseif (Input::has('search-breed')) 
    {
        $dogs = Dog::whereHas('breed', function($q)     
        {
            $queryString = Input::get('search-breed');
            $q->where('name', 'LIKE', "%$queryString%");

        })->orderBy('name')->paginate(5);

    } //end elseif

    else {
        $dogs = Dog::orderBy('name')->paginate(5);
        } //end else

    return View::make('dogs.index')->with(array('dogs' => $dogs));
} //end function index()

当我在20英里范围内搜索狮子狗,男性时,网址显示如下:

http://ruff-love.dev/dogs?search-breed=poodle&sex=M&miles=20

当搜索只是品种时,搜索目前正常。

我似乎无法弄清楚添加SEX和RADIUS标准的语法。 它应该允许这些条件为null并仍然执行查询。

任何建议都会非常适用

3 个答案:

答案 0 :(得分:22)

您可以使用查询范围http://laravel.com/docs/eloquent#query-scopes使其在控制器中(或将来在您将要执行的任何地方)变得冗长和简单,然后根据您的需要将其链接起来:

// Dog model
public function scopeSearchBreed($query, $breed)
{
  $query->whereHas('breed', function ($q) use ($breed) {
    $q->where('name', 'like', "%{$breed}%");
  });
}

public function scopeWithinRadius($query, $radius)
{
  $query->where(...); // do math here
}

然后您需要的就是:

public function index()
{
  $q = Dog::query();

  if (Input::has('search'))
  {
     // simple where here or another scope, whatever you like
     $q->where('name','like',Input::get('search'));
  }

  if (Input::has('search-breed'))
  {
     $q->searchBreed(Input::get('search-breed'));
  }

  if (Input::has('sex'))
  {
     $q->where('sex', Input::get('sex'));
  }

  if (Input::has('radius'))
  {
     $q->withinRadius(Input::get('radius'));
  }

  $dogs = $q->orderBy(..)->paginate(5);

  // ...

答案 1 :(得分:2)

这是一个可能的解决方案,我认为可能还有其他解决方案。使用query()函数创建一个空查询构建器,并为其添加非null子句,然后在最后调用paginate()函数。

$builder = Dogs::query();
if (Input::has('search')) {
    $queryString = Input::get('search');
    $builder->where('name', 'LIKE', "%$queryString%");
}
// ... more clauses from the querystring
$dogs = $builder->orderBy('name')->paginate(5);

答案 2 :(得分:1)

$builder = Dogs::query();
$term = Request::all();
if(!empty($term['breed'])){
    $builder->where('breed','=',$term['breed']);
}
if(!empty($term['sex'])){
    $builder->where('sex','=',$term['sex']);
}
if(!empty($term['radius'])){
    $builder->where('radius','=',$term['radius']);
}

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