Dept search in User query scopes

This commit is contained in:
snipe 2017-05-23 02:49:27 -07:00
parent e0d2cbef20
commit aeca549bab

View file

@ -24,7 +24,7 @@ class User extends SnipeModel implements AuthenticatableContract, CanResetPasswo
protected $hidden = ['password']; protected $hidden = ['password'];
protected $table = 'users'; protected $table = 'users';
protected $injectUniqueIdentifier = true; 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 = [ protected $casts = [
'activated' => 'boolean', 'activated' => 'boolean',
@ -106,6 +106,11 @@ class User extends SnipeModel implements AuthenticatableContract, CanResetPasswo
return $this->belongsTo('\App\Models\Company', 'company_id'); return $this->belongsTo('\App\Models\Company', 'company_id');
} }
public function department()
{
return $this->belongsTo('\App\Models\Department', 'department_id');
}
public function isActivated() public function isActivated()
{ {
return $this->activated ==1; return $this->activated ==1;
@ -358,6 +363,11 @@ class User extends SnipeModel implements AuthenticatableContract, CanResetPasswo
$query->where('locations.name', 'LIKE', '%'.$search.'%'); $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) { ->orWhere(function ($query) use ($search) {
$query->whereHas('groups', function ($query) use ($search) { $query->whereHas('groups', function ($query) use ($search) {
$query->where('groups.name', 'LIKE', '%'.$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); 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);
}
} }