Skip to content
Advertisement

im having problem with join on multi table in laravel

working on group chat web app with laravel

i want to perform search for user on particular group with their name

so basically there are three table

users -> id , timeline_id , email , pw

timelines -> id , fname , lname , p_no

page_user(defines user on particular group/page) -> id , page_id , user_id

page -> id , timeline_id, pg_name

when i perform search on one selected group , query giving search from whole user not from selected group

`$page_members= DB::table('timelines')`
     `->join('users', 'timelines.id', '=', 'users.timeline_id')`    
     `->where('timelines.fname','LIKE','%'.$q.'%')`        ``
     `->orWhere('timelines.lname','LIKE','%'.$q.'%')`
     `->orWhere('users.email','LIKE','%'.$q.'%')`
     `->orderby('users.id', 'DESC')`
     `->get();`

where i need to do change ; considering that $page_id = 1; $q is search value;

Advertisement

Answer

$page_members= DB::table('timelines')
      ->join('users', 'timelines.id', '=', 'users.timeline_id')
      ->join('page_user', 'page_user.user_id', '=', 'users.id')
      ->select('users.*','timelines.*', 'timelines.name', 'timelines.lastname','timelines.username')
       ->where('timelines.name','LIKE','%'.$q.'%')
       ->where('page_user.page_id','=',$pgid)
       ->orWhere('timelines.lastname','LIKE','%'.$q.'%')
       ->where('page_user.page_id','=',$pgid)
       ->orWhere('users.primarycontactno','LIKE','%'.$q.'%')
       ->where('page_user.page_id','=',$pgid)
       ->orderby('users.id', 'DESC')
       ->get();

it worked but dont know why ->where('page_user.page_id','=',$pgid) required more time , is that I did something wrong? if i put this on first place only then wont work for timeline.lastname and users.primarycontactno , if anyone has optimized solution do share;)

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement