Skip to content

Instantly share code, notes, and snippets.

@edomaru
Created June 1, 2016 04:20
Show Gist options
  • Save edomaru/d9eaa3376f5c8a340b622763c3553532 to your computer and use it in GitHub Desktop.
Save edomaru/d9eaa3376f5c8a340b622763c3553532 to your computer and use it in GitHub Desktop.
fix contact manager search issue
public function index(Request $request)
    {        
        $userId   = $request->user()->id;
        $groupId  = $request->get('group_id');
        $keywords = '%' . $request->get('term') . '%';

        $contacts = Contact::where(function($query) use ($userId, $groupId, $keywords) {
                            // filter by current user
                            $query->where("user_id", $userId);
                            // filter by selected group
                            if ($groupId) $query->where('group_id', $groupId);
                                                                                    
                            $query->where("name", 'LIKE', $keywords);  

                        })
                        ->orWhere(function($query) use ($userId, $groupId, $keywords) {
                            // filter by current user
                            $query->where("user_id", $userId);
                            // filter by selected group
                            if ($groupId) $query->where('group_id', $groupId);
                                                                                    
                            $query->where("email", 'LIKE', $keywords);  
                        })
                        ->orWhere(function($query) use ($userId, $groupId, $keywords) {
                            // filter by current user
                            $query->where("user_id", $userId);
                            // filter by selected group
                            if ($groupId) $query->where('group_id', $groupId);
                                                                                    
                            $query->where("company", 'LIKE', $keywords);  
                        })
                        ->orderBy('id', 'desc')
                        ->paginate($this->limit);
                        
    	return view('contacts.index', compact('contacts'));
    }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment