Skip to content

A free open source IT asset/license management system

Dept search in User query scopes

snipeaeca549bab02
diff --git a/app/Models/User.php b/app/Models/User.php
index d7909f1420..42c584fef2 100755
--- a/app/Models/User.php
+++ b/app/Models/User.php
@@ -24,7 +24,7 @@ class User extends SnipeModel implements AuthenticatableContract, CanResetPasswo
protected $hidden = ['password'];
protected $table = 'users';
protected $injectUniqueIdentifier = true;
- protected $fillable = ['first_name', 'last_name', 'email','password','username'];
+ protected $fillable = ['first_name', 'last_name', 'email','password','username','department_id'];
protected $casts = [
'activated' => 'boolean',
@@ -106,6 +106,11 @@ class User extends SnipeModel implements AuthenticatableContract, CanResetPasswo
return $this->belongsTo('\App\Models\Company', 'company_id');
}
+ public function department()
+ {
+ return $this->belongsTo('\App\Models\Department', 'department_id');
+ }
+
public function isActivated()
{
return $this->activated ==1;
@@ -358,6 +363,11 @@ class User extends SnipeModel implements AuthenticatableContract, CanResetPasswo
$query->where('locations.name', 'LIKE', '%'.$search.'%');
});
})
+ ->orWhere(function ($query) use ($search) {
+ $query->whereHas('department', function ($query) use ($search) {
+ $query->where('departments.name', 'LIKE', '%'.$search.'%');
+ });
+ })
->orWhere(function ($query) use ($search) {
$query->whereHas('groups', function ($query) use ($search) {
$query->where('groups.name', 'LIKE', '%'.$search.'%');
@@ -413,4 +423,18 @@ class User extends SnipeModel implements AuthenticatableContract, CanResetPasswo
{
return $query->leftJoin('locations', 'users.location_id', '=', 'locations.id')->orderBy('locations.name', $order);
}
+
+
+ /**
+ * Query builder scope to order on department
+ *
+ * @param Illuminate\Database\Query\Builder $query Query builder instance
+ * @param text $order Order
+ *
+ * @return Illuminate\Database\Query\Builder Modified query builder
+ */
+ public function scopeOrderDepartment($query, $order)
+ {
+ return $query->leftJoin('departments', 'users.department_id', '=', 'departments.id')->orderBy('departments.name', $order);
+ }
}