5.1 空值字段查询怎么写

查询字段 name,如果请求name为空值,以下写法无效,求答案!
$name = $request->input("name");
$email = $request->input("email");

$sql =DB::table('users')->where(function (加粗文字$query) {
if(isset($name)){
$query->where('name', 2);
}
})->toSql();
已邀请:

yuzhahui

赞同来自:

经查找得,分页带条件查询,但是有没有更好的写法???????
$user =DB::table('users')->where(function ($query) use ($request){
$name = $request->input("name");
$email = $request->input("email");

if(!empty(Trim($name))){

$query->where('name','like','%'.$name.'%');
}
if(!empty(Trim($email))){
$query->where('email','like','%'.$email.'%');
}
})->paginate($rp);

要回复问题请先登录注册