Laravel Eloquent 搜索两个可选字段

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15167770/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 07:35:23  来源:igfitidea点击:

Laravel Eloquent search two optional fields

phplaraveleloquent

提问by tmartin314

I'm trying to search two optional tables using eloquent:

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

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

This works fine for return all results when a user does an empty search, but I am not sure how to adjust this for to search for bandname when that is present and vise versa.

当用户进行空搜索时,这适用于返回所有结果,但我不知道如何调整它以在存在时搜索bandname,反之亦然。

回答by vFragosop

Just to explain what happens on answer below:

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

Eloquent does a tricky thing here: When you call User::where(...)it returns a Database\ Queryobject. This is basically the same thing as DB::table('users')->where(...), a chainable object for constructing SQL queries.

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

So having:

所以有:

// 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();

回答by tmartin314

Building on Vinicius' answer here's what worked:

以 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();

Turns out that the second optional field must use or_where only if $artist is not set.

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

Thanks for your help

谢谢你的帮助

回答by dynamo

I think this is what youre after. Your view would have a form to search artist/genre one or the other can be set, or both, or none.

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

$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();