mirror of
https://github.com/snipe/snipe-it.git
synced 2024-11-20 04:24:23 -08:00
baa3be728d
* Adds the ability to search by dates Adding extra „where“-conditions to the „TextSearch“ queries, allowing the users to search by dates * Adds missing dates to $dates in models * Removes duplicated „where“ conditions * Adds the Searchable trait to models, defining the searchable attributes and relations * Removes the old text search methods * Adds back additional conditions to the search These conditions could not be modeled in the „attributes“ or „relations“, so we include them here * Removes unnecessary check for the deleted_at attribute * Fixes typo in comments * suppresses errors from Codacy We can safely ignore the error codacy is throwing here, since this method is a standin/noop for models who need to implement more advanced searches
125 lines
3.3 KiB
PHP
125 lines
3.3 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use App\Http\Traits\UniqueUndeletedTrait;
|
|
use App\Models\Traits\Searchable;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Log;
|
|
use Watson\Validating\ValidatingTrait;
|
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
|
use App\Models\SnipeModel;
|
|
use App\Models\User;
|
|
|
|
class Department extends SnipeModel
|
|
{
|
|
/**
|
|
* Whether the model should inject it's identifier to the unique
|
|
* validation rules before attempting validation. If this property
|
|
* is not set in the model it will default to true.
|
|
*
|
|
* @var boolean
|
|
*/
|
|
protected $injectUniqueIdentifier = true;
|
|
|
|
use ValidatingTrait, UniqueUndeletedTrait;
|
|
|
|
protected $rules = [
|
|
'name' => 'required|max:255',
|
|
'user_id' => 'required',
|
|
'location_id' => 'numeric|nullable',
|
|
'company_id' => 'numeric|nullable',
|
|
'manager_id' => 'numeric|nullable',
|
|
];
|
|
|
|
/**
|
|
* The attributes that are mass assignable.
|
|
*
|
|
* @var array
|
|
*/
|
|
protected $fillable = [
|
|
'user_id',
|
|
'name',
|
|
'location_id',
|
|
'company_id',
|
|
'manager_id',
|
|
'notes',
|
|
];
|
|
|
|
use Searchable;
|
|
|
|
/**
|
|
* The attributes that should be included when searching the model.
|
|
*
|
|
* @var array
|
|
*/
|
|
protected $searchableAttributes = ['name', 'notes'];
|
|
|
|
/**
|
|
* The relations and their attributes that should be included when searching the model.
|
|
*
|
|
* @var array
|
|
*/
|
|
protected $searchableRelations = [];
|
|
|
|
|
|
public function company()
|
|
{
|
|
return $this->belongsTo('\App\Models\Company', 'company_id');
|
|
}
|
|
|
|
/**
|
|
* Even though we allow allow for checkout to things beyond users
|
|
* this method is an easy way of seeing if we are checked out to a user.
|
|
* @return mixed
|
|
*/
|
|
public function users()
|
|
{
|
|
return $this->hasMany('\App\Models\User', 'department_id');
|
|
}
|
|
|
|
|
|
/**
|
|
* Return the manager in charge of the dept
|
|
* @return mixed
|
|
*/
|
|
public function manager()
|
|
{
|
|
return $this->belongsTo('\App\Models\User', 'manager_id');
|
|
}
|
|
|
|
|
|
public function location()
|
|
{
|
|
return $this->belongsTo('\App\Models\Location', 'location_id');
|
|
}
|
|
|
|
/**
|
|
* Query builder scope to order on location name
|
|
*
|
|
* @param \Illuminate\Database\Query\Builder $query Query builder instance
|
|
* @param text $order Order
|
|
*
|
|
* @return \Illuminate\Database\Query\Builder Modified query builder
|
|
*/
|
|
public function scopeOrderLocation($query, $order)
|
|
{
|
|
return $query->leftJoin('locations as department_location', 'departments.location_id', '=', 'department_location.id')->orderBy('department_location.name', $order);
|
|
}
|
|
|
|
/**
|
|
* Query builder scope to order on manager name
|
|
*
|
|
* @param \Illuminate\Database\Query\Builder $query Query builder instance
|
|
* @param text $order Order
|
|
*
|
|
* @return \Illuminate\Database\Query\Builder Modified query builder
|
|
*/
|
|
public function scopeOrderManager($query, $order)
|
|
{
|
|
return $query->leftJoin('users as department_user', 'departments.manager_id', '=', 'department_user.id')->orderBy('department_user.first_name', $order)->orderBy('department_user.last_name', $order);
|
|
}
|
|
|
|
|
|
}
|