Skip to content

A free open source IT asset/license management system

Fixed #17670 - advanced search on relationships not working

snipe4ae4af5c1034
diff --git a/app/Http/Controllers/Api/AssetsController.php b/app/Http/Controllers/Api/AssetsController.php
index e3bd87bb26..1a34e3f62f 100644
--- a/app/Http/Controllers/Api/AssetsController.php
+++ b/app/Http/Controllers/Api/AssetsController.php
@@ -116,6 +116,22 @@ class AssetsController extends Controller
'asset_eol_date',
'requestable',
'jobtitle',
+ // These are *relationships* so we wouldn't normally include them in this array,
+ // since they would normally create a `column not found` error,
+ // BUT we account for them in the ordering switch down at the end of this method
+ // DO NOT ADD ANYTHING TO THIS LIST WITHOUT CHECKING THE ORDERING SWITCH BELOW!
+ 'company',
+ 'model',
+ 'location',
+ 'rtd_location',
+ 'category',
+ 'status_label',
+ 'manufacturer',
+ 'supplier',
+ 'jobtitle',
+ 'assigned_to',
+ 'created_by',
+
];
$all_custom_fields = CustomField::all(); //used as a 'cache' of custom fields throughout this page load
@@ -132,6 +148,7 @@ class AssetsController extends Controller
$filter = array_filter($filter, function ($key) use ($allowed_columns) {
return in_array($key, $allowed_columns);
}, ARRAY_FILTER_USE_KEY);
+
}
$assets = Asset::select('assets.*')
diff --git a/app/Models/Asset.php b/app/Models/Asset.php
index 9ef8cd28c9..70941f62e9 100644
--- a/app/Models/Asset.php
+++ b/app/Models/Asset.php
@@ -1881,16 +1881,32 @@ class Asset extends Depreciable
);
}
- if ($fieldname =='assigned_to') {
+ 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.last_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.'%');
+ }
+ );
+ }
);
}