如何在 laravel 5.3 中的 where 子句中使用 OR 和 AND?
How to use OR and AND with where clause in laravel 5.3?
-- 我要使用的查询:
select * from users where (email like '%email@email.com%' OR username like 'myusername') AND (password like '%1234@abcde%')
-- 我的上述查询的 laravel 代码是:
$getInfo = Users::where('password','like','%'.$request->password.'%')
->where(function($query){
$query->where('email','like','%'.$request->username.'%')
->orWhere('username','like','%'.$req->username.'%');
})->get();
这里出现错误未定义的变量:请求
您需要将 $request
传递给您的闭包函数,例如 use ($request)
$getInfo = Users::where('password','like','%'.$request->password.'%')
->where(function($query) use ($request){
$query->where('email','like','%'.$request->username.'%')
->orWhere('username','like','%'.$req->username.'%');
})->get();
此外,使用 like 和两边的通配符 (%) 来搜索用户详细信息也不是一个好主意,应该只是 like
不带通配符或者只使用相等比较 =
$getInfo = Users::where('password','like','%'.$request->password.'%')
->where(function($query) use ($request){
$query->where('email','like','%'.$request->username.'%')
->orWhere('username','like','%'.$request->username.'%');
})->get();
您可以使用 use ($request)
.
来关闭变量
你也有错别字
改变
->orWhere('username','like','%'.$req->username.'%');
到
->orWhere('username','like','%'.$request->username.'%');
-- 我要使用的查询:
select * from users where (email like '%email@email.com%' OR username like 'myusername') AND (password like '%1234@abcde%')
-- 我的上述查询的 laravel 代码是:
$getInfo = Users::where('password','like','%'.$request->password.'%')
->where(function($query){
$query->where('email','like','%'.$request->username.'%')
->orWhere('username','like','%'.$req->username.'%');
})->get();
这里出现错误未定义的变量:请求
您需要将 $request
传递给您的闭包函数,例如 use ($request)
$getInfo = Users::where('password','like','%'.$request->password.'%')
->where(function($query) use ($request){
$query->where('email','like','%'.$request->username.'%')
->orWhere('username','like','%'.$req->username.'%');
})->get();
此外,使用 like 和两边的通配符 (%) 来搜索用户详细信息也不是一个好主意,应该只是 like
不带通配符或者只使用相等比较 =
$getInfo = Users::where('password','like','%'.$request->password.'%')
->where(function($query) use ($request){
$query->where('email','like','%'.$request->username.'%')
->orWhere('username','like','%'.$request->username.'%');
})->get();
您可以使用 use ($request)
.
你也有错别字
改变
->orWhere('username','like','%'.$req->username.'%');
到
->orWhere('username','like','%'.$request->username.'%');