Skip to content

A free open source IT asset/license management system

Fix for compound is:not_null

snipef4bd2a68c987
diff --git a/app/Models/Traits/Searchable.php b/app/Models/Traits/Searchable.php
index f328cf2a98..72eca12bfa 100644
--- a/app/Models/Traits/Searchable.php
+++ b/app/Models/Traits/Searchable.php
@@ -757,7 +757,26 @@ trait Searchable
$searchableRelations = $this->getSearchableRelations();
$resolvedRelationKey = $this->resolveSearchableRelationKey($filterKey, $searchableRelations);
- if ($resolvedRelationKey !== null && ! $this->isAssignedToRelationKey($resolvedRelationKey)) {
+ if ($resolvedRelationKey !== null && $this->isAssignedToRelationKey($resolvedRelationKey)) {
+ $method = $boolean === 'or' ? 'orWhere' : 'where';
+ // Polymorphic assignment is present only when both columns are set; null matches either side missing.
+
+ if ($isNull) {
+ $query->{$method}(function (Builder $assigneeNullQuery) use ($table): void {
+ $assigneeNullQuery->whereNull($table.'.assigned_to')
+ ->orWhereNull($table.'.assigned_type');
+ });
+ } else {
+ $query->{$method}(function (Builder $assigneeNotNullQuery) use ($table): void {
+ $assigneeNotNullQuery->whereNotNull($table.'.assigned_to')
+ ->whereNotNull($table.'.assigned_type');
+ });
+ }
+
+ return $query;
+ }
+
+ if ($resolvedRelationKey !== null) {
if ($isNull) {
$method = $boolean === 'or' ? 'orDoesntHave' : 'doesntHave';
$query->{$method}($resolvedRelationKey);
diff --git a/tests/Feature/Search/SearchableTraitTest.php b/tests/Feature/Search/SearchableTraitTest.php
index 4ef1feef0c..1669f49f52 100644
--- a/tests/Feature/Search/SearchableTraitTest.php
+++ b/tests/Feature/Search/SearchableTraitTest.php
@@ -855,6 +855,41 @@ class SearchableTraitTest extends TestCase
$this->assertNotContains((int) $withoutSupplier->id, $returnedIds2);
}
+ /**
+ * Regression: `assigned_to` is a polymorphic searchable relation key.
+ * `is:null` should return unassigned assets; `is:not_null` should return assigned assets.
+ */
+ public function test_is_null_filter_on_polymorphic_assigned_to_relation_key()
+ {
+ /** @var User $assignee */
+ $assignee = User::factory()->create();
+ $assignedAsset = Asset::factory()->assignedToUser($assignee)->create();
+ $unassignedAsset = Asset::factory()->create([
+ 'assigned_to' => null,
+ 'assigned_type' => null,
+ ]);
+
+ $superuser = User::factory()->viewAssets()->create();
+
+ $response = $this->actingAsForApi($superuser)
+ ->getJson(route('api.assets.index', ['filter' => json_encode(['assigned_to' => 'is:null'])]))
+ ->assertOk();
+
+ $returnedNullIds = collect($response->json('rows'))->pluck('id')->map(fn ($id) => (int) $id)->all();
+
+ $this->assertContains((int) $unassignedAsset->id, $returnedNullIds);
+ $this->assertNotContains((int) $assignedAsset->id, $returnedNullIds);
+
+ $response2 = $this->actingAsForApi($superuser)
+ ->getJson(route('api.assets.index', ['filter' => json_encode(['assigned_to' => 'is:not_null'])]))
+ ->assertOk();
+
+ $returnedNotNullIds = collect($response2->json('rows'))->pluck('id')->map(fn ($id) => (int) $id)->all();
+
+ $this->assertContains((int) $assignedAsset->id, $returnedNotNullIds);
+ $this->assertNotContains((int) $unassignedAsset->id, $returnedNotNullIds);
+ }
+
/**
* Test custom field partial match via filter.
*/