laravel组合查询生成器

时间:2016-04-05 07:52:06

标签: php mysql laravel

SELECT * FROM users
WHERE  `dateofjoining` >=  '2016-03-05'
AND  `dateofjoining` <=  '2016-03-09'
AND (
`login_username` LIKE  '%bala%'
OR  `phoneno` LIKE  '%987%'
)
LIMIT 0 , 30

Laravel查询:

但是在我的laravel查询中,login_username的某个时间设置为null,而我的phonenumber也将某个时间设置为null。在下面的查询中,我收到错误的laravel&#34; $ username&#34;变量是未定的。

$lists = Users::where ( function ( $query ) {
              $query->where('login_username','like','%'.$username.'%')
                    ->or_where("phoneno", "LIKE", '%'.$username.'%');
         })->whereBetween('dateofjoining',array($fromDate, $toDate))  
         ->paginate(5);

我的查询中有错误吗?

2 个答案:

答案 0 :(得分:0)

您的查询中没有错误,但您必须扩展在扩展位置内使用的变量范围(请参阅第一行代码中的更改,应添加use()和内部使用的所有变量,以将其范围扩展到闭合

$lists = Users::where ( function ( $query ) use ($username) {
              $query->where('login_username','like','%'.$username.'%')
                    ->or_where("phoneno", "LIKE", '%'.$username.'%');
         })->whereBetween('dateofjoining',array($fromDate, $toDate))  
         ->paginate(5);

答案 1 :(得分:0)

问题在于你的情况,
只需用以下代码替换您的代码:

$lists = Users::where ( function ( $query ) USE ($username) {
              $query->where('login_username','like','%'.$username.'%')
                     ->orWhere('phoneno', "LIKE", '%'.$username.'%');
         })->whereBetween('dateofjoining',array($fromDate, $toDate))  
         ->paginate(5);