如何将普通搜索查询转换为laravel查询生成器

时间:2017-09-13 07:26:48

标签: php mysql laravel-5

我已经编写了一个普通的搜索查询,它运行正常,但我有 在我的项目中实现laravel分页但是laravel分页不能正常查询,因此我想使用查询构建器将我的搜索查询转换为laravel查询。

我的普通搜索查询是: -

 $query="SELECT t.* FROM (SELECT users.`id`,users.`email`,users.`u_firstname`,users.`u_lastname`,users.`u_dob`,users.`u_mobile`,user_roles.`ur_role_id`,UCASE(master_roles.`role_name`) as roles,case users.`u_status` when '0' then 'Inactive' when '1' then 'Active' when '4' then 'Deactivate' end as status,users.`u_status` FROM `users` inner join user_roles on user_roles.`ur_user_id`=users.`id` inner join master_roles on user_roles.`ur_role_id`=master_roles.`role_id`) t WHERE (t.status LIKE '$search%' or t.u_firstname like '$search%' or t.u_lastname like '$search%' or t.email like '$search%' or t.u_mobile like '$search%' or t.u_dob like '$search%' or t.roles like '$search%') and t.id!='$id' and t.u_status!='3'";

我想以这种形式转换我的查询我已经获取了我必须应用搜索的所有结果但我没有得到如何应用所有列的搜索ia无法在下面的查询中集成搜索概念,如用户键入varun然后它将从all列搜索并仅给出结果: -

   $post = DB::table('users')
                ->join('user_roles','users.id','=','user_roles.ur_user_id')
                ->join('master_roles','user_roles.ur_role_id','=','master_roles.role_id')
                ->select('users.id','users.email','users.u_firstname','users.u_lastname','users.u_dob','users.u_mobile','user_roles.ur_role_id','master_roles.role_name as roles',DB::raw('(CASE WHEN users.u_status ="0"  THEN "Inactive"  when "1" then "Active" END) AS status'))
                ->where('users.id','!=',$id)
                ->get();

运行上面的laravel查询后,我得到了输出,现在我必须 应用搜索部分; -

..............................................................................
 id     email          u_firstname     u_lastname     roles      status  
 ..........................................................................
  1   a@gmail.com        a              b          seller         Active
  2   b@gmail.com        c              d          Buyer          Inactive

2 个答案:

答案 0 :(得分:0)

您可以像这样使用orWhere

$query = DB::table('users')
    ->join('user_roles','users.id','=','user_roles.ur_user_id')
    ->join('master_roles','user_roles.ur_role_id','=','master_roles.role_id');
    $query->where(function($query)use($search){
        $query->orwhere('t.status','LIKE','%'.trim($search).'%');
        $query->orwhere('t.u_firstname','LIKE','%'.trim($search).'%');
        $query->orwhere('t.u_lastname','LIKE','%'.trim($search).'%');
        $query->orwhere('t.email','LIKE','%'.trim($search).'%');
        $query->orwhere('t.u_mobile','LIKE','%'.trim($search).'%');
        $query->orwhere('t.u_dob','LIKE','%'.trim($search).'%');
        $query->orwhere('t.roles','LIKE','%'.trim($search).'%');
    });

$query->select('users.id','users.email','users.u_firstname','users.u_lastname','users.u_dob','users.u_mobile','user_roles.ur_role_id','master_roles.role_name as roles',DB::raw('(CASE WHEN users.u_status ="0"  THEN "Inactive"  when "1" then "Active" END) AS status'));
$query->where('users.id','!=',$id);
$query->where('t.u_status','!=',3);
$post = $query->get();

答案 1 :(得分:0)

您可以自己创建Paginator - check the pagination manual

另外我建议使用full-text search甚至更好的自定义搜索引擎代替LIKE = '%text%'