Skip to content

Commit 2cb3717

Browse files
author
Quentin Schmick
authored
Merge pull request #20 from bluefyn-international/chore/null-contains
Updating contains and does not cotains to handle null
2 parents 883e67a + 68d4bd7 commit 2cb3717

File tree

2 files changed

+17
-4
lines changed

2 files changed

+17
-4
lines changed

src/BaseFeatures/Filters/ContainsFilter.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace BluefynInternational\ReportEngine\BaseFeatures\Filters;
44

55
use Illuminate\Database\Query\Builder;
6+
use Illuminate\Support\Facades\DB;
67

78
class ContainsFilter extends BaseFilter
89
{
@@ -25,9 +26,9 @@ public function apply(Builder $builder, array $options = []) : Builder
2526
*
2627
* @return Builder
2728
*/
28-
public static function build(Builder $builder, $field, $value, string $action = 'where') : Builder
29+
public static function build(Builder $builder, mixed $field, mixed $value, string $action = 'where') : Builder
2930
{
30-
return $builder->$action($field, 'like', '%' . $value . '%');
31+
return $builder->$action(DB::raw('COALESCE(' . $field . ", '')"), 'like', '%' . $value . '%');
3132
}
3233

3334
/**

src/BaseFeatures/Filters/DoesNotContainFilter.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace BluefynInternational\ReportEngine\BaseFeatures\Filters;
44

55
use Illuminate\Database\Query\Builder;
6+
use Illuminate\Support\Facades\DB;
67

78
class DoesNotContainFilter extends BaseFilter
89
{
@@ -14,9 +15,20 @@ class DoesNotContainFilter extends BaseFilter
1415
*/
1516
public function apply(Builder $builder, array $options = []) : Builder
1617
{
17-
$action = $this->getAction();
18+
return self::build($builder, $this->getField(), $this->getValue(), $this->getAction());
19+
}
1820

19-
return $builder->$action($this->getField(), 'not like', '%' . $this->getValue() . '%');
21+
/**
22+
* @param Builder $builder
23+
* @param mixed $field
24+
* @param mixed $value
25+
* @param string $action
26+
*
27+
* @return Builder
28+
*/
29+
public static function build(Builder $builder, mixed $field, mixed $value, string $action = 'where') : Builder
30+
{
31+
return $builder->$action(DB::raw('COALESCE(' . $field . ", '')"), 'not like', '%' . $value . '%');
2032
}
2133

2234
/**

0 commit comments

Comments
 (0)