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

垃圾109g

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() }} 

控制器页面

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英里内输入对狮子狗的搜索时,URL显示如下:

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

当仅搜索品种时,搜索当前可以正常进行。

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

任何建议将不胜感激

杰瑞克·特卡奇克(Jarek Tkaczyk)

您可以使用查询范围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);

  // ...

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章