我使用Laravel雄辩的查询构建器,我有一个查询,我想在多个条件上有一个where子句。它能起作用,但并不优雅。

例子:

$results = User::where('this', '=', 1)
    ->where('that', '=', 1)
    ->where('this_too', '=', 1)
    ->where('that_too', '=', 1)
    ->where('this_as_well', '=', 1)
    ->where('that_as_well', '=', 1)
    ->where('this_one_too', '=', 1)
    ->where('that_one_too', '=', 1)
    ->where('this_one_as_well', '=', 1)
    ->where('that_one_as_well', '=', 1)
    ->get();

有没有更好的方法,或者我应该坚持这个方法?


当前回答

$projects = DB::table('projects')->where([['title','like','%'.$input.'%'],
    ['status','<>','Pending'],
    ['status','<>','Not Available']])
->orwhere([['owner', 'like', '%'.$input.'%'],
    ['status','<>','Pending'],
    ['status','<>','Not Available']])->get();

其他回答

public function search()
{
    if (isset($_GET) && !empty($_GET))
    {
        $prepareQuery = '';
        foreach ($_GET as $key => $data)
        {
            if ($data)
            {
                $prepareQuery.=$key . ' = "' . $data . '" OR ';
            }
        }
        $query = substr($prepareQuery, 0, -3);
        if ($query)
            $model = Businesses::whereRaw($query)->get();
        else
            $model = Businesses::get();

        return view('pages.search', compact('model', 'model'));
    }
}

根据我的建议,如果你正在做筛选或搜索

那么你应该选择:

        $results = User::query();
        $results->when($request->that, function ($q) use ($request) {
            $q->where('that', $request->that);
        });
        $results->when($request->this, function ($q) use ($request) {
            $q->where('this', $request->that);
        });
        $results->when($request->this_too, function ($q) use ($request) {
            $q->where('this_too', $request->that);
        });
        $results->get();

多个where子句

    $query=DB::table('users')
        ->whereRaw("users.id BETWEEN 1003 AND 1004")
        ->whereNotIn('users.id', [1005,1006,1007])
        ->whereIn('users.id',  [1008,1009,1010]);
    $query->where(function($query2) use ($value)
    {
        $query2->where('user_type', 2)
            ->orWhere('value', $value);
    });

   if ($user == 'admin'){
        $query->where('users.user_name', $user);
    }

终于得到了结果

    $result = $query->get();

你可以在几种情况下使用,

$results = User::where([
    ['column_name1', '=', $value1],
    ['column_name2', '<', $value2],
    ['column_name3', '>', $value3]
])->get();

你也可以这样用,

$results = User::orderBy('id','DESC');
$results = $results->where('column1','=', $value1);
$results = $results->where('column2','<',  $value2);
$results = $results->where('column3','>',  $value3);
$results = $results->get();

多个地方

您还可以将一个条件数组传递给where函数。数组的每个元素都应该是一个数组,包含通常传递给where方法的三个参数:

Model::where([
    ['status', '=', '1'],
    ['subscribed', '<>', '1'],
])->get();

多个或Where子句

如果你需要在圆括号内对"or"条件进行分组,你可以将闭包作为第一个参数传递给orWhere方法:

Model::where('votes', '>', 100)
        ->orWhere(function ($query) {
            $query->where('name', 'abcd')
                ->orWhere(function ($query) {
                    $query->where('name', 'cdef')
                        ->where('votes', '>', 50);
                });
        })
        ->get();

上面的例子将产生以下SQL语句:

select * from `users` where `votes` > 100 or (`name` = "abcd" or (`name` = "cdef" and `votes` > 50));

多指针where with whereNot和orWhereNot方法

whereNot和orWhereNot方法可用于对给定的查询约束组求反。例如,以下查询排除清仓或价格小于10的产品:

$products = Product::where([
        ['status', '=', '1'],
        ['is_feature', '1']
    ])
        ->whereNot(function ($query) {
            $query->where('clearance', true)
                ->orWhere('price', '<', 10);
        })
        ->get();