mercredi 13 septembre 2017

How to convert normal search query in to laravel query builder

I have written a normal search query and it is working properly fine but i have to implement laravel pagination in my project but laravel pagination is not working with normal query hence i want to convert my search query in to laravel query using query builder.

My Normal search query is:-

 $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'";

I want to convert my query in this form i have fetched the all result on which i have to applied searching but i am not getting how to applied search with all columns i a not able to integrate searching concept in the below query like user type varun then it will search from the all column and give only that result:-

   $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();

after runing the above laravel query i got below output and now i have to applied searching part in it;-

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



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire