【问题标题】:Laravel Eloquent search two optional fieldsLaravel Eloquent 搜索两个可选字段
【发布时间】:2013-03-01 22:14:55
【问题描述】:

我正在尝试使用 eloquent 搜索两个可选表:

$users  = User::where('ProfileType', '=', 2)
                ->where(function($query) {
                    $query->where('BandName', 'LIKE', "%$artist%");
                    $query->or_where('Genre', 'LIKE', "%$genre%");
                })->get();

当用户进行空搜索时,这适用于返回所有结果,但我不确定如何调整它以在存在乐队名称时搜索乐队名称,反之亦然。

【问题讨论】:

  • 那么查询是否总是:WHERE ProfileType = 2 AND (BandName = 'foo' OR Genre = 'bar')?我试图弄清楚哪些搜索值将始终存在,哪些可能不存在。
  • 是的,ProfileType 将始终存在,但 BandName 和 Genre 都是可选的,甚至可以留空。谢谢

标签: php laravel eloquent


【解决方案1】:

只是为了解释下面的答案会发生什么:

Eloquent 在这里做了一件棘手的事情:当您调用 User::where(...) 时,它会返回一个 Database\ Query 对象。这与DB::table('users')->where(...) 基本相同,是用于构造SQL 查询的可链接对象。

所以有:

// Instantiates a Query object
$query = User::where('ProfileType', '=', '2');
$query->where(function($query) {
    // Adds a clause to the query
    if ($artist = Input::get('artist')) {
        $query->where_nested('BandName', 'LIKE', "%$artist%", 'OR');
    }
    // And another
    if ($genre = Input::get('genre')) {
        $query->where_nested('Genre', 'LIKE', "%$genre%", 'OR');
    }
});

// Executes the query and fetches it's results
$users = $query->get();

【讨论】:

  • 这个答案看起来不错,但由于某种原因没有找到任何结果。不知道是什么问题
  • 哦,我没有意识到您想用 OR 搜索多个字段。通常每个字段都充当过滤器。在这种情况下,AND 是要走的路。我将编辑答案并提供一些说明。
  • 这可以在一个输入字段中完成吗?例如一个名称过滤器,它检查过滤器是否等于 firstname OR lastname...
【解决方案2】:

以 Vinicius 的回答为基础,这就是有效的方法:

// Instantiates a Query object
$query = User::where('ProfileType', '=', '2');

// Adds a clause to the query
if ($artist = Input::get('artist')) {
    $query->where('BandName', 'LIKE', "%$artist%");
    // Temp Usernamesearch
    $query->or_where('NickName', 'LIKE', "%$artist%");
}

// Genre - switch function if artist is not empty
if ($genre = Input::get('genre')) {
    $func = ($artist) ? 'or_where' : 'where';
    $query->$func('Genre', 'LIKE', "%$genre%");
}

// Executes the query and fetches it's results
$users = $query->get();

事实证明,只有在未设置 $artist 时,第二个可选字段才必须使用 or_where。

感谢您的帮助

【讨论】:

    【解决方案3】:

    我认为这就是你所追求的。您的视图将有一个表单来搜索艺术家/流派,可以设置一个或另一个,或两者都设置,或不设置。

    $users = User::where('ProfileType', '=', 2);
    
    if (Input::has('artist')) {
        $users = $users->where('BandName', 'LIKE', '%'.Input::get('artist').'%');
    }
    
    if (Input::has('genre')) {
        $users = $users->where('Genre', 'LIKE', '%'.Input::get('genre').'%');
    }
    
    $users = $users->get();
    

    【讨论】:

      【解决方案4】:
      $query = FormEntry::with('form')->where('domain_id', $id);
      
      $query->where(function($query) use ($search, $start, $limit, $order, $dir) {                
           $query->where('first_name', 'LIKE', "%{$search}%")
                 ->orWhere('last_name', 'LIKE', "%{$search}%")
                 ->orWhere('email', 'LIKE', "%{$search}%")
                 ->offset($start)
                 ->limit($limit)
                 ->orderBy($order, $dir);
            });
      
      $entries = $query->get();
      $totalFiltered = $query->count();
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-03-12
        • 2014-05-19
        • 2021-09-12
        • 1970-01-01
        • 2015-02-07
        • 1970-01-01
        相关资源
        最近更新 更多