Skip to content

Commit

Permalink
Fix Relation filter input on zero (0) value fields (#157)
Browse files Browse the repository at this point in the history
  • Loading branch information
pushpak authored May 11, 2021
1 parent 1cda293 commit 628a694
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
11 changes: 8 additions & 3 deletions src/ModelFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use Illuminate\Database\Eloquent\Builder as QueryBuilder;
use Illuminate\Database\Eloquent\Relations\Relation;
use Illuminate\Support\Arr;
use Illuminate\Support\Str;

/**
Expand Down Expand Up @@ -435,8 +434,14 @@ public function getRelatedFilterInput($related)
foreach ((array) $this->relations[$related] as $alias => $name) {
// If the alias is a string that is what we grab from the input
// Then use the name for the output so we can alias relations
if ($value = Arr::get($this->input, is_string($alias) ? $alias : $name)) {
$output[$name] = $value;
$keyName = is_string($alias) ? $alias : $name;

if (array_key_exists($keyName, $this->input)) {
$keyValue = $this->input[$keyName];

if ($this->includeFilterInput($keyName, $keyValue)) {
$output[$name] = $keyValue;
}
}
}
}
Expand Down
8 changes: 6 additions & 2 deletions tests/ModelFilterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -167,11 +167,15 @@ public function testRelatedMethodBooleans()
public function testGetFilterInputForRelationsArray()
{
$this->filter->relations = [
'roles' => ['roles'],
'roles' => ['roles', 'enabled', 'flagged'],
];
$this->filter->push($this->testInput);

$this->assertEquals($this->filter->getRelatedFilterInput('roles'), ['roles' => $this->filter->input('roles')]);
$this->filter->push(['enabled' => 0]);

$this->filter->push(['flagged' => 1]);

$this->assertEquals($this->filter->getRelatedFilterInput('roles'), ['roles' => $this->filter->input('roles'), 'enabled' => $this->filter->input('enabled'), 'flagged' => $this->filter->input('flagged')]);
}

/**
Expand Down

0 comments on commit 628a694

Please sign in to comment.