Skip to content

A free open source IT asset/license management system

One more try I guess

snipe762ea9b4dbbe
diff --git a/app/Models/Traits/Searchable.php b/app/Models/Traits/Searchable.php
index 72eca12bfa..27ac7748b0 100644
--- a/app/Models/Traits/Searchable.php
+++ b/app/Models/Traits/Searchable.php
@@ -700,14 +700,21 @@ trait Searchable
$dbColumn = $this->resolveCustomFieldDbColumn($filterKey);
if ($dbColumn !== null) {
- $method = match (true) {
- $isNull && $boolean === 'or' => 'orWhereNull',
- $isNull => 'whereNull',
- $boolean === 'or' => 'orWhereNotNull',
- default => 'whereNotNull',
- };
+ $column = $table.'.'.$dbColumn;
- $query->{$method}($table.'.'.$dbColumn);
+ $method = $boolean === 'or' ? 'orWhere' : 'where';
+
+ $query->{$method}(function (Builder $subQuery) use ($column, $isNull): void {
+ if ($isNull) {
+ $subQuery->whereNull($column)
+ ->orWhere($column, '=', '');
+
+ return;
+ }
+
+ $subQuery->whereNotNull($column)
+ ->where($column, '!=', '');
+ });
return $query;
}
@@ -715,14 +722,20 @@ trait Searchable
// Direct attribute column.
if (in_array($filterKey, $searchableAttributes, true)) {
- $method = match (true) {
- $isNull && $boolean === 'or' => 'orWhereNull',
- $isNull => 'whereNull',
- $boolean === 'or' => 'orWhereNotNull',
- default => 'whereNotNull',
- };
-
- $query->{$method}($table.'.'.$filterKey);
+ $column = $table.'.'.$filterKey;
+ $method = $boolean === 'or' ? 'orWhere' : 'where';
+
+ $query->{$method}(function (Builder $subQuery) use ($column, $isNull): void {
+ if ($isNull) {
+ $subQuery->whereNull($column)
+ ->orWhere($column, '=', '');
+
+ return;
+ }
+
+ $subQuery->whereNotNull($column)
+ ->where($column, '!=', '');
+ });
return $query;
}
diff --git a/tests/Feature/Search/SearchableTraitTest.php b/tests/Feature/Search/SearchableTraitTest.php
index 1669f49f52..2f35f58a8d 100644
--- a/tests/Feature/Search/SearchableTraitTest.php
+++ b/tests/Feature/Search/SearchableTraitTest.php
@@ -789,6 +789,32 @@ class SearchableTraitTest extends TestCase
$this->assertNotContains((int) $withoutNotes->id, $returnedIds2);
}
+ /**
+ * Blank string values should be treated like empty content for direct string fields.
+ */
+ public function test_is_not_null_filter_excludes_blank_string_direct_attributes()
+ {
+ $populated = Asset::factory()->create([
+ 'name' => 'Named Asset '.now()->timestamp,
+ 'order_number' => 'PO-12345',
+ ]);
+ $blank = Asset::factory()->create([
+ 'name' => '',
+ 'order_number' => '',
+ ]);
+
+ $superuser = User::factory()->viewAssets()->create();
+
+ $response = $this->actingAsForApi($superuser)
+ ->getJson(route('api.assets.index', ['filter' => json_encode(['order_number' => 'is:not_null'])]))
+ ->assertOk();
+
+ $returnedIds = collect($response->json('rows'))->pluck('id')->map(fn ($id) => (int) $id)->all();
+
+ $this->assertContains((int) $populated->id, $returnedIds);
+ $this->assertNotContains((int) $blank->id, $returnedIds);
+ }
+
/**
* "is:not_null" on the User virtual "name" column should match users where
* at least one constituent column (first_name, last_name) is not null.