Moved adminuser method to the SnipeModel

This commit is contained in:
snipe
2026-03-27 19:20:54 +00:00
parent 4545cf8989
commit 80b7ebd508
16 changed files with 108 additions and 751 deletions
+2 -244
View File
@@ -38,13 +38,13 @@ class Asset extends Depreciable
use CompanyableTrait;
use HasFactory;
use HasUploads;
use Loggable;
use Presentable;
use Requestable;
use SoftDeletes;
use UniqueUndeletedTrait;
use ValidatingTrait;
use HasUploads;
public const LOCATION = 'location';
@@ -959,20 +959,6 @@ class Asset extends Depreciable
->orderBy('created_at', 'desc');
}
/**
* Get user who created the item
*
* @author [A. Gianotto] [<snipe@snipe.net>]
*
* @since [v1.0]
*
* @return Relation
*/
public function adminuser()
{
return $this->belongsTo(User::class, 'created_by')->withTrashed();
}
/**
* Establishes the asset -> status relationship
*
@@ -1332,6 +1318,7 @@ class Asset extends Depreciable
/**
* Run additional, advanced searches.
* This overrides the advancedTextSearch method on the Searchable model trait to add searching of assigned user, location, and assets.
*
* @param array $terms The search terms
* @return Builder
@@ -1896,235 +1883,6 @@ class Asset extends Depreciable
)->withTrashed()->whereNull('assets.deleted_at'); // workaround for laravel bug
}
/**
* Query builder scope to search on text filters for complex Bootstrap Tables API
*
* @param \Illuminate\Database\Query\Builder $query Query builder instance
* @param text $filter JSON array of search keys and terms
* @return \Illuminate\Database\Query\Builder Modified query builder
*/
public function scopeByFilter($query, $filter)
{
return $query->where(
function ($query) use ($filter) {
foreach ($filter as $key => $search_val) {
$fieldname = str_replace('custom_fields.', '', $key);
if ($fieldname == 'asset_tag') {
$query->where('assets.asset_tag', 'LIKE', '%'.$search_val.'%');
}
if ($fieldname == 'name') {
$query->where('assets.name', 'LIKE', '%'.$search_val.'%');
}
if ($fieldname == 'serial') {
$query->where('assets.serial', 'LIKE', '%'.$search_val.'%');
}
if ($fieldname == 'purchase_date') {
$query->where('assets.purchase_date', 'LIKE', '%'.$search_val.'%');
}
if ($fieldname == 'purchase_cost') {
$query->where('assets.purchase_cost', 'LIKE', '%'.$search_val.'%');
}
if ($fieldname == 'notes') {
$query->where('assets.notes', 'LIKE', '%'.$search_val.'%');
}
if ($fieldname == 'order_number') {
$query->where('assets.order_number', 'LIKE', '%'.$search_val.'%');
}
if ($fieldname == 'status_label') {
$query->whereHas(
'assetstatus', function ($query) use ($search_val) {
$query->where('status_labels.name', 'LIKE', '%'.$search_val.'%');
}
);
}
if ($fieldname == 'location') {
$query->whereHas(
'location', function ($query) use ($search_val) {
$query->where('locations.name', 'LIKE', '%'.$search_val.'%');
}
);
}
if ($fieldname == 'rtd_location') {
$query->whereHas(
'defaultLoc', function ($query) use ($search_val) {
$query->where('locations.name', 'LIKE', '%'.$search_val.'%');
}
);
}
if ($fieldname == 'assigned_to') {
$query->whereHasMorph(
'assignedTo', [User::class], function ($query) use ($search_val) {
$query->where(
function ($query) use ($search_val) {
$query->where('users.first_name', 'LIKE', '%'.$search_val.'%')
->orWhere('users.last_name', 'LIKE', '%'.$search_val.'%')
->orWhere('users.display_name', 'LIKE', '%'.$search_val.'%')
->orWhere('users.username', 'LIKE', '%'.$search_val.'%');
}
);
}
)->orWhereHasMorph(
'assignedTo', [Location::class], function ($query) use ($search_val) {
$query->where('locations.name', 'LIKE', '%'.$search_val.'%');
}
)->orWhereHasMorph(
'assignedTo', [Asset::class], function ($query) use ($search_val) {
$query->where(
function ($query) use ($search_val) {
// Don't use the asset table prefix here because it will pull from the original asset,
// not the subselect we're doing here to get the assigned asset
$query->where('name', 'LIKE', '%'.$search_val.'%')
->orWhere('asset_tag', 'LIKE', '%'.$search_val.'%');
}
);
}
);
}
if ($fieldname == 'manufacturer') {
$query->whereHas(
'model', function ($query) use ($search_val) {
$query->whereHas(
'manufacturer', function ($query) use ($search_val) {
$query->where(
function ($query) use ($search_val) {
$query->where('manufacturers.name', 'LIKE', '%'.$search_val.'%');
}
);
}
);
}
);
}
if ($fieldname == 'category') {
$query->whereHas(
'model', function ($query) use ($search_val) {
$query->whereHas(
'category', function ($query) use ($search_val) {
$query->where(
function ($query) use ($search_val) {
$query->where('categories.name', 'LIKE', '%'.$search_val.'%')
->orWhere('models.name', 'LIKE', '%'.$search_val.'%')
->orWhere('models.model_number', 'LIKE', '%'.$search_val.'%');
}
);
}
);
}
);
}
if ($fieldname == 'model') {
$query->whereHas(
'model', function ($query) use ($search_val) {
$query->where('models.name', 'LIKE', '%'.$search_val.'%');
}
);
}
if ($fieldname == 'model_number') {
$query->whereHas(
'model', function ($query) use ($search_val) {
$query->where('models.model_number', 'LIKE', '%'.$search_val.'%');
}
);
}
if ($fieldname == 'company') {
$query->whereHas(
'company', function ($query) use ($search_val) {
$query->where('companies.name', 'LIKE', '%'.$search_val.'%');
}
);
}
if ($fieldname == 'supplier') {
$query->whereHas(
'supplier', function ($query) use ($search_val) {
$query->where('suppliers.name', 'LIKE', '%'.$search_val.'%');
}
);
}
if ($fieldname == 'status_label') {
$query->whereHas(
'assetstatus', function ($query) use ($search_val) {
$query->where('status_labels.name', 'LIKE', '%'.$search_val.'%');
}
);
}
if ($fieldname == 'jobtitle') {
$query->where(function ($query) use ($search_val) {
if (is_array($search_val)) {
$query->whereHasMorph(
'assignedTo',
[User::class],
function ($query) use ($search_val) {
$query->whereIn('users.jobtitle', $search_val);
}
);
} else {
$query->whereHasMorph(
'assignedTo',
[User::class],
function ($query) use ($search_val) {
$query->where(function ($query) use ($search_val) {
$query->where('users.jobtitle', 'LIKE', '%'.$search_val.'%');
});
}
);
}
});
}
/**
* THIS CLUNKY BIT IS VERY IMPORTANT
*
* Although inelegant, this section matters a lot when querying against fields that do not
* exist on the asset table. There's probably a better way to do this moving forward, for
* example using the Schema:: methods to determine whether or not a column actually exists,
* or even just using the $searchableRelations variable earlier in this file.
*
* In short, this set of statements tells the query builder to ONLY query against an
* actual field that's being passed if it doesn't meet known relational fields. This
* allows us to query custom fields directly in the assets table
* (regardless of their name) and *skip* any fields that we already know can only be
* searched through relational searches that we do earlier in this method.
*
* For example, we do not store "location" as a field on the assets table, we store
* that relationship through location_id on the assets table, therefore querying
* assets.location would fail, as that field doesn't exist -- plus we're already searching
* against those relationships earlier in this method.
*
* - snipe
*/
if (($fieldname != 'category') && ($fieldname != 'model_number') && ($fieldname != 'rtd_location') && ($fieldname != 'location') && ($fieldname != 'supplier')
&& ($fieldname != 'status_label') && ($fieldname != 'assigned_to') && ($fieldname != 'model') && ($fieldname != 'jobtitle') && ($fieldname != 'company') && ($fieldname != 'manufacturer')
) {
$query->where('assets.'.$fieldname, 'LIKE', '%'.$search_val.'%');
}
}
}
);
}
/**
* Query builder scope to order on model
*
+3 -61
View File
@@ -100,6 +100,8 @@ class AssetModel extends SnipeModel
'depreciation' => ['name'],
'category' => ['name'],
'manufacturer' => ['name'],
'fieldset' => ['name'],
'adminuser' => ['first_name', 'last_name', 'display_name'],
];
protected static function booted(): void
@@ -147,6 +149,7 @@ class AssetModel extends SnipeModel
if ($this->availableAssets()->count() == 0) {
return 0;
}
return $this->availableAssets()->count() / $this->assets()->count() * 100;
}
@@ -261,73 +264,12 @@ class AssetModel extends SnipeModel
&& ($this->deleted_at == '');
}
/**
* Get user who created the item
*
* @author [A. Gianotto] [<snipe@snipe.net>]
*
* @since [v1.0]
*
* @return Relation
*/
public function adminuser()
{
return $this->belongsTo(User::class, 'created_by')->withTrashed();
}
/**
* -----------------------------------------------
* BEGIN QUERY SCOPES
* -----------------------------------------------
**/
/**
* Query builder scope to search on text filters for complex Bootstrap Tables API
*
* @param Builder $query Query builder instance
* @param text $filter JSON array of search keys and terms
* @return Builder Modified query builder
*/
public function scopeByFilter($query, $filter)
{
return $query->where(
function ($query) use ($filter) {
foreach ($filter as $fieldname => $search_val) {
if ($fieldname == 'name') {
$query->where('models.name', 'LIKE', '%'.$search_val.'%');
}
if ($fieldname == 'notes') {
$query->where('models.notes', 'LIKE', '%'.$search_val.'%');
}
if ($fieldname == 'model_number') {
$query->where('models.model_number', 'LIKE', '%'.$search_val.'%');
}
if ($fieldname == 'category') {
$query->whereHas(
'category', function ($query) use ($search_val) {
$query->where('categories.name', 'LIKE', '%'.$search_val.'%');
}
);
}
if ($fieldname == 'manufacturer') {
$query->whereHas(
'manufacturer', function ($query) use ($search_val) {
$query->where('manufacturers.name', 'LIKE', '%'.$search_val.'%');
}
);
}
}
}
);
}
/**
* scopeInCategory
* Get all models that are in the array of category ids
+9 -34
View File
@@ -89,14 +89,21 @@ class Category extends SnipeModel
*
* @var array
*/
protected $searchableAttributes = ['name', 'category_type', 'notes'];
protected $searchableAttributes = [
'name',
'category_type',
'notes',
'eula_text',
];
/**
* The relations and their attributes that should be included when searching the model.
*
* @var array
*/
protected $searchableRelations = [];
protected $searchableRelations = [
'adminuser' => ['first_name', 'last_name', 'display_name'],
];
/**
* Checks if category can be deleted
@@ -263,11 +270,6 @@ class Category extends SnipeModel
return $this->hasMany(AssetModel::class, 'category_id');
}
public function adminuser()
{
return $this->belongsTo(User::class, 'created_by')->withTrashed();
}
/**
* Checks for a category-specific EULA, and if that doesn't exist,
* checks for a settings level EULA
@@ -315,33 +317,6 @@ class Category extends SnipeModel
* -----------------------------------------------
**/
/**
* Query builder scope to search on text filters for complex Bootstrap Tables API
*
* @param Builder $query Query builder instance
* @param text $filter JSON array of search keys and terms
* @return Builder Modified query builder
*/
public function scopeByFilter($query, $filter)
{
return $query->where(
function ($query) use ($filter) {
foreach ($filter as $fieldname => $search_val) {
if ($fieldname == 'name') {
$query->where('categories.name', 'LIKE', '%'.$search_val.'%');
}
if ($fieldname == 'category_type') {
$query->where('categories.category_type', 'LIKE', '%'.$search_val.'%');
}
}
}
);
}
/**
* Query builder scope for whether or not the category requires acceptance
*
+12 -7
View File
@@ -60,14 +60,24 @@ final class Company extends SnipeModel
*
* @var array
*/
protected $searchableAttributes = ['name', 'phone', 'fax', 'email', 'created_at', 'updated_at'];
protected $searchableAttributes = [
'name',
'phone',
'fax',
'email',
'created_at',
'updated_at',
'notes',
];
/**
* The relations and their attributes that should be included when searching the model.
*
* @var array
*/
protected $searchableRelations = [];
protected $searchableRelations = [
'adminuser' => ['first_name', 'last_name', 'display_name'],
];
/**
* The attributes that are mass assignable.
@@ -317,11 +327,6 @@ final class Company extends SnipeModel
}
public function adminuser()
{
return $this->belongsTo(User::class, 'created_by')->withTrashed();
}
/**
* I legit do not know what this method does, but we can't remove it (yet).
*
+1 -103
View File
@@ -115,6 +115,7 @@ class Component extends SnipeModel
'location' => ['name'],
'supplier' => ['name'],
'manufacturer' => ['name'],
'adminuser' => ['first_name', 'last_name', 'display_name'],
];
public static function booted()
@@ -164,22 +165,6 @@ class Component extends SnipeModel
return $this->belongsToMany(Asset::class, 'components_assets')->withPivot('id', 'assigned_qty', 'created_at', 'created_by', 'note');
}
/**
* Establishes the component -> admin user relationship
*
* @todo this is probably not needed - refactor
*
* @author [A. Gianotto] [<snipe@snipe.net>]
*
* @since [v3.0]
*
* @return Relation
*/
public function adminuser()
{
return $this->belongsTo(User::class, 'created_by')->withTrashed();
}
/**
* Establishes the component -> company relationship
*
@@ -410,93 +395,6 @@ class Component extends SnipeModel
* -----------------------------------------------
**/
/**
* Query builder scope to search on text filters for complex Bootstrap Tables API
*
* @param Builder $query Query builder instance
* @param text $filter JSON array of search keys and terms
* @return Builder Modified query builder
*/
public function scopeByFilter($query, $filter)
{
return $query->where(
function ($query) use ($filter) {
foreach ($filter as $fieldname => $search_val) {
if ($fieldname == 'name') {
$query->where('components.name', 'LIKE', '%'.$search_val.'%');
}
if ($fieldname == 'notes') {
$query->where('components.notes', 'LIKE', '%'.$search_val.'%');
}
if ($fieldname == 'model_number') {
$query->where('components.model_number', 'LIKE', '%'.$search_val.'%');
}
if ($fieldname == 'order_number') {
$query->where('components.order_number', 'LIKE', '%'.$search_val.'%');
}
if ($fieldname == 'serial') {
$query->where('components.serial', 'LIKE', '%'.$search_val.'%');
}
if ($fieldname == 'serial') {
$query->where('components.serial', 'LIKE', '%'.$search_val.'%');
}
if ($fieldname == 'purchase_cost') {
$query->where('components.purchase_cost', 'LIKE', '%'.$search_val.'%');
}
if ($fieldname == 'location') {
$query->whereHas(
'location', function ($query) use ($search_val) {
$query->where('locations.name', 'LIKE', '%'.$search_val.'%');
}
);
}
if ($fieldname == 'manufacturer') {
$query->whereHas(
'manufacturer', function ($query) use ($search_val) {
$query->where('manufacturers.name', 'LIKE', '%'.$search_val.'%');
}
);
}
if ($fieldname == 'supplier') {
$query->whereHas(
'supplier', function ($query) use ($search_val) {
$query->where('suppliers.name', 'LIKE', '%'.$search_val.'%');
}
);
}
if ($fieldname == 'category') {
$query->whereHas(
'category', function ($query) use ($search_val) {
$query->where('categories.name', 'LIKE', '%'.$search_val.'%');
}
);
}
if ($fieldname == 'company') {
$query->whereHas(
'company', function ($query) use ($search_val) {
$query->where('companies.name', 'LIKE', '%'.$search_val.'%');
}
);
}
}
}
);
}
/**
* Query builder scope to order on company
*
+11 -95
View File
@@ -96,7 +96,15 @@ class Consumable extends SnipeModel
*
* @var array
*/
protected $searchableAttributes = ['name', 'order_number', 'purchase_cost', 'purchase_date', 'item_no', 'model_number', 'notes'];
protected $searchableAttributes = [
'name',
'order_number',
'purchase_cost',
'purchase_date',
'item_no',
'model_number',
'notes',
];
/**
* The relations and their attributes that should be included when searching the model.
@@ -109,6 +117,7 @@ class Consumable extends SnipeModel
'location' => ['name'],
'manufacturer' => ['name'],
'supplier' => ['name'],
'adminuser' => ['first_name', 'last_name', 'display_name'],
];
/**
@@ -141,20 +150,6 @@ class Consumable extends SnipeModel
&& ($this->deleted_at == '');
}
/**
* Establishes the consumable -> admin user relationship
*
* @author [A. Gianotto] [<snipe@snipe.net>]
*
* @since [v3.0]
*
* @return Relation
*/
public function adminuser()
{
return $this->belongsTo(User::class, 'created_by')->withTrashed();
}
/**
* Establishes the component -> assignments relationship
*
@@ -174,6 +169,7 @@ class Consumable extends SnipeModel
if ($this->consumables_users_count == 0) {
return 100;
}
return ($this->qty - $this->consumables_users_count) / $this->qty * 100;
}
@@ -186,7 +182,6 @@ class Consumable extends SnipeModel
*
* @return Relation
*/
public function company()
{
return $this->belongsTo(Company::class, 'company_id');
@@ -417,85 +412,6 @@ class Consumable extends SnipeModel
* @param text $filter JSON array of search keys and terms
* @return Builder Modified query builder
*/
public function scopeByFilter($query, $filter)
{
return $query->where(
function ($query) use ($filter) {
foreach ($filter as $fieldname => $search_val) {
if ($fieldname == 'name') {
$query->where('consumables.name', 'LIKE', '%'.$search_val.'%');
}
if ($fieldname == 'notes') {
$query->where('consumables.notes', 'LIKE', '%'.$search_val.'%');
}
if ($fieldname == 'model_number') {
$query->where('consumables.model_number', 'LIKE', '%'.$search_val.'%');
}
if ($fieldname == 'order_number') {
$query->where('consumables.order_number', 'LIKE', '%'.$search_val.'%');
}
if ($fieldname == 'item_no') {
$query->where('consumables.item_no', 'LIKE', '%'.$search_val.'%');
}
if ($fieldname == 'serial') {
$query->where('consumables.serial', 'LIKE', '%'.$search_val.'%');
}
if ($fieldname == 'purchase_cost') {
$query->where('consumables.purchase_cost', 'LIKE', '%'.$search_val.'%');
}
if ($fieldname == 'location') {
$query->whereHas(
'location', function ($query) use ($search_val) {
$query->where('locations.name', 'LIKE', '%'.$search_val.'%');
}
);
}
if ($fieldname == 'manufacturer') {
$query->whereHas(
'manufacturer', function ($query) use ($search_val) {
$query->where('manufacturers.name', 'LIKE', '%'.$search_val.'%');
}
);
}
if ($fieldname == 'supplier') {
$query->whereHas(
'supplier', function ($query) use ($search_val) {
$query->where('suppliers.name', 'LIKE', '%'.$search_val.'%');
}
);
}
if ($fieldname == 'category') {
$query->whereHas(
'category', function ($query) use ($search_val) {
$query->where('categories.name', 'LIKE', '%'.$search_val.'%');
}
);
}
if ($fieldname == 'company') {
$query->whereHas(
'company', function ($query) use ($search_val) {
$query->where('companies.name', 'LIKE', '%'.$search_val.'%');
}
);
}
}
}
);
}
/**
* Query builder scope to order on company
+24
View File
@@ -88,6 +88,30 @@ class CustomField extends Model
'show_in_requestable_list',
];
/**
* The attributes that should be included when searching the model.
*
* @var array
*/
protected $searchableAttributes = [
'name',
'format',
'element',
'db_column',
'help_text',
];
/**
* The relations and their attributes that should be included when searching the model.
*
* @var array
*/
protected $searchableRelations = [
'fieldset' => ['name'],
'assetModels' => ['name'],
'adminuser' => ['first_name', 'last_name', 'display_name'],
];
/**
* This is confusing, since it's actually the custom fields table that
* we're usually modifying, but since we alter the assets table, we have to
+9 -2
View File
@@ -77,14 +77,21 @@ class Department extends SnipeModel
*
* @var array
*/
protected $searchableAttributes = ['name', 'notes', 'phone', 'fax'];
protected $searchableAttributes = [
'name',
'notes',
'phone',
'fax',
];
/**
* The relations and their attributes that should be included when searching the model.
*
* @var array
*/
protected $searchableRelations = [];
protected $searchableRelations = [
'adminuser' => ['first_name', 'last_name', 'display_name'],
];
public function isDeletable()
{
+11 -3
View File
@@ -40,7 +40,10 @@ class Depreciation extends SnipeModel
*
* @var array
*/
protected $fillable = ['name', 'months'];
protected $fillable = [
'name',
'months',
];
use Searchable;
@@ -49,14 +52,19 @@ class Depreciation extends SnipeModel
*
* @var array
*/
protected $searchableAttributes = ['name', 'months'];
protected $searchableAttributes = [
'name',
'months',
];
/**
* The relations and their attributes that should be included when searching the model.
*
* @var array
*/
protected $searchableRelations = [];
protected $searchableRelations = [
'adminuser' => ['first_name', 'last_name', 'display_name'],
];
public function isDeletable()
{
+9 -17
View File
@@ -35,6 +35,7 @@ class Group extends SnipeModel
* @var bool
*/
protected $injectUniqueIdentifier = true;
protected $presenter = GroupPresenter::class;
use Searchable;
@@ -45,15 +46,20 @@ class Group extends SnipeModel
*
* @var array
*/
protected $searchableAttributes = ['name', 'created_at', 'notes'];
protected $searchableAttributes = [
'name',
'created_at',
'notes',
];
/**
* The relations and their attributes that should be included when searching the model.
*
* @var array
*/
protected $searchableRelations = [];
protected $searchableRelations = [
'adminuser' => ['first_name', 'last_name', 'display_name'],
];
public function isDeletable()
{
@@ -75,20 +81,6 @@ class Group extends SnipeModel
return $this->belongsToMany(User::class, 'users_groups');
}
/**
* Get the user that created the group
*
* @author A. Gianotto <snipe@snipe.net>
*
* @since [v6.3.0]
*
* @return Relation
*/
public function adminuser()
{
return $this->belongsTo(User::class, 'created_by')->withTrashed();
}
/**
* Decode JSON permissions into array
*
+3 -14
View File
@@ -112,6 +112,8 @@ class License extends Depreciable
'purchase_cost',
'purchase_date',
'expiration_date',
'license_email',
'license_name',
];
/**
@@ -125,6 +127,7 @@ class License extends Depreciable
'category' => ['name'],
'depreciation' => ['name'],
'supplier' => ['name'],
'adminuser' => ['first_name', 'last_name', 'display_name'],
];
protected $appends = ['free_seat_count'];
@@ -520,20 +523,6 @@ class License extends Depreciable
->orderBy('created_at', 'desc');
}
/**
* Establishes the license -> admin user relationship
*
* @author A. Gianotto <snipe@snipe.net>
*
* @since [v2.0]
*
* @return Relation
*/
public function adminuser()
{
return $this->belongsTo(User::class, 'created_by')->withTrashed();
}
/**
* Returns the total number of all license seats
*
+1 -12
View File
@@ -115,6 +115,7 @@ class Location extends SnipeModel
protected $searchableRelations = [
'parent' => ['name'],
'company' => ['name'],
'adminuser' => ['first_name', 'last_name', 'display_name'],
];
/**
@@ -158,18 +159,6 @@ class Location extends SnipeModel
return $this->hasMany(User::class, 'location_id');
}
/**
* Establishes the location -> admin user relationship
*
* @author A. Gianotto <snipe@snipe.net>
*
* @return Relation
*/
public function adminuser()
{
return $this->belongsTo(User::class, 'created_by')->withTrashed();
}
/**
* Find assets with this location as their location_id
*
+1 -15
View File
@@ -94,6 +94,7 @@ class Maintenance extends SnipeModel implements ICompanyableChild
'asset.supplier' => ['name'],
'asset.assetstatus' => ['name'],
'supplier' => ['name'],
'adminuser' => ['first_name', 'last_name', 'display_name'],
];
public function getCompanyableParents()
@@ -195,21 +196,6 @@ class Maintenance extends SnipeModel implements ICompanyableChild
->withTrashed();
}
/**
* Get the admin who created the maintenance
*
* @return mixed
*
* @author A. Gianotto <snipe@snipe.net>
*
* @version v3.0
*/
public function adminuser()
{
return $this->belongsTo(User::class, 'created_by')
->withTrashed();
}
public function supplier()
{
return $this->belongsTo(Supplier::class, 'supplier_id')
+8 -7
View File
@@ -67,14 +67,20 @@ class Manufacturer extends SnipeModel
*
* @var array
*/
protected $searchableAttributes = ['name', 'created_at', 'notes'];
protected $searchableAttributes = [
'name',
'created_at',
'notes',
];
/**
* The relations and their attributes that should be included when searching the model.
*
* @var array
*/
protected $searchableRelations = [];
protected $searchableRelations = [
'adminuser' => ['first_name', 'last_name', 'display_name'],
];
public function isDeletable()
{
@@ -117,11 +123,6 @@ class Manufacturer extends SnipeModel
return $this->hasMany(Component::class, 'manufacturer_id');
}
public function adminuser()
{
return $this->belongsTo(User::class, 'created_by')->withTrashed();
}
/**
* Query builder scope to order on the user that created it
*/
+3 -6
View File
@@ -143,12 +143,9 @@ class PredefinedKit extends SnipeModel
*
* @var array
*/
protected $searchableRelations = [];
public function adminuser()
{
return $this->belongsTo(User::class, 'created_by');
}
protected $searchableRelations = [
'adminuser' => ['first_name', 'last_name', 'display_name'],
];
/**
* Establishes the kits -> models relationship
+1 -131
View File
@@ -167,6 +167,7 @@ class User extends SnipeModel implements AuthenticatableContract, AuthorizableCo
'groups' => ['name'],
'company' => ['name'],
'manager' => ['first_name', 'last_name', 'username', 'display_name'],
'adminuser' => ['first_name', 'last_name', 'display_name'],
];
/**
@@ -948,137 +949,6 @@ class User extends SnipeModel implements AuthenticatableContract, AuthorizableCo
return new \stdClass;
}
/**
* Query builder scope to search on text filters for complex Bootstrap Tables API
*
* @param \Illuminate\Database\Query\Builder $query Query builder instance
* @param text $filter JSON array of search keys and terms
* @return \Illuminate\Database\Query\Builder Modified query builder
*/
public function scopeByFilter($query, $filter)
{
return $query->where(
function ($query) use ($filter) {
foreach ($filter as $fieldname => $search_val) {
if ($fieldname == 'first_name') {
$query->where('users.first_name', 'LIKE', '%'.$search_val.'%');
}
if ($fieldname == 'last_name') {
$query->where('users.last_name', 'LIKE', '%'.$search_val.'%');
}
if ($fieldname == 'display_name') {
$query->where('users.display_name', 'LIKE', '%'.$search_val.'%');
}
if ($fieldname == 'name') {
$query->where('users.last_name', 'LIKE', '%'.$search_val.'%')
->orWhere('users.first_name', 'LIKE', '%'.$search_val.'%');
}
if ($fieldname == 'username') {
$query->where('users.username', 'LIKE', '%'.$search_val.'%');
}
if ($fieldname == 'email') {
$query->where('users.email', 'LIKE', '%'.$search_val.'%');
}
if ($fieldname == 'phone') {
$query->where('users.phone', 'LIKE', '%'.$search_val.'%');
}
if ($fieldname == 'mobile') {
$query->where('users.mobile', 'LIKE', '%'.$search_val.'%');
}
if ($fieldname == 'phone') {
$query->where('users.phone', 'LIKE', '%'.$search_val.'%');
}
if ($fieldname == 'jobtitle') {
$query->where('users.jobtitle', 'LIKE', '%'.$search_val.'%');
}
if ($fieldname == 'created_at') {
$query->where('users.created_at', '=', '%'.$search_val.'%');
}
if ($fieldname == 'updated_at') {
$query->where('users.updated_at', '=', '%'.$search_val.'%');
}
if ($fieldname == 'start_date') {
$query->where('users.start_date', '=', '%'.$search_val.'%');
}
if ($fieldname == 'end_date') {
$query->where('users.end_date', '=', '%'.$search_val.'%');
}
if ($fieldname == 'employee_num') {
$query->where('users.employee_num', 'LIKE', '%'.$search_val.'%');
}
if ($fieldname == 'locale') {
$query->where('users.locale', 'LIKE', '%'.$search_val.'%');
}
if ($fieldname == 'address') {
$query->where('users.address', 'LIKE', '%'.$search_val.'%');
}
if ($fieldname == 'state') {
$query->where('users.state', 'LIKE', '%'.$search_val.'%');
}
if ($fieldname == 'zip') {
$query->where('users.zip', 'LIKE', '%'.$search_val.'%');
}
if ($fieldname == 'country') {
$query->where('users.country', 'LIKE', '%'.$search_val.'%');
}
if ($fieldname == 'vip') {
$query->where('users.vip', 'LIKE', '%'.$search_val.'%');
}
if ($fieldname == 'remote') {
$query->where('users.remote', 'LIKE', '%'.$search_val.'%');
}
if ($fieldname == 'start_date') {
$query->where('users.purchase_date', 'LIKE', '%'.$search_val.'%');
}
if ($fieldname == 'notes') {
$query->where('users.notes', 'LIKE', '%'.$search_val.'%');
}
if ($fieldname == 'location') {
$query->whereHas(
'location', function ($query) use ($search_val) {
$query->where('locations.name', 'LIKE', '%'.$search_val.'%');
}
);
}
if ($fieldname == 'company') {
$query->whereHas(
'company', function ($query) use ($search_val) {
$query->where('companies.name', 'LIKE', '%'.$search_val.'%');
}
);
}
}
}
);
}
/**
* Query builder scope to search user by name with spaces in it.
* We don't use the advancedTextSearch() scope because that searches