From f065bd77849f22acf7ec93e90c287701ee9772fb Mon Sep 17 00:00:00 2001 From: snipe Date: Tue, 5 Mar 2019 20:40:05 -0800 Subject: [PATCH] Added ability to do full name search in user dropdown selectlist --- app/Http/Controllers/Api/UsersController.php | 3 +-- app/Models/User.php | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/app/Http/Controllers/Api/UsersController.php b/app/Http/Controllers/Api/UsersController.php index 8783679b9b..e91ae8f676 100644 --- a/app/Http/Controllers/Api/UsersController.php +++ b/app/Http/Controllers/Api/UsersController.php @@ -148,8 +148,7 @@ class UsersController extends Controller $users = Company::scopeCompanyables($users); if ($request->has('search')) { - $users = $users->where('first_name', 'LIKE', '%'.$request->get('search').'%') - ->orWhere('last_name', 'LIKE', '%'.$request->get('search').'%') + $users = $users->SimpleNameSearch($request->get('search')) ->orWhere('username', 'LIKE', '%'.$request->get('search').'%') ->orWhere('employee_num', 'LIKE', '%'.$request->get('search').'%'); } diff --git a/app/Models/User.php b/app/Models/User.php index c783bdbba5..15e29657d7 100755 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -413,6 +413,25 @@ class User extends SnipeModel implements AuthenticatableContract, CanResetPasswo return json_decode($this->permissions, true); } + /** + * Query builder scope to search user by name with spaces in it. + * We don't use the advancedTextSearch() scope because that searches + * all of the relations as well, which is more than what we need. + * + * @param \Illuminate\Database\Query\Builder $query Query builder instance + * @param array $terms The search terms + * @return \Illuminate\Database\Query\Builder + */ + public function scopeSimpleNameSearch($query, $search) { + + $query = $query->where('first_name', 'LIKE', '%'.$search.'%') + ->orWhere('last_name', 'LIKE', '%'.$search.'%') + ->orWhereRaw('CONCAT('.DB::getTablePrefix().'users.first_name," ",'.DB::getTablePrefix().'users.last_name) LIKE ?', ["%$search%", "%$search%"]); + return $query; + } + + + /** * Run additional, advanced searches. *