Skip to content

Commit

Permalink
Add alias support (#111)
Browse files Browse the repository at this point in the history
* Add related model method alias support (closes #94)
  • Loading branch information
Tucker-Eric authored Dec 24, 2019
1 parent 7f6eb9c commit 02055b3
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 4 deletions.
33 changes: 33 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -534,6 +534,39 @@ class ClientFilter extends ModelFilter
}
}
```
##### `$relations` array alias support
The `$relations` array supports aliases. This is used when the input doesn't match the related model's filter method.
This will transform the input keys being passed to the related model filter's input.

##### Example:
```php
class UserFilter extends ModelFilter
{
public $relations = [
'clients' => [
'client_industry' => 'industry',
'client_potential' => 'potential_volume'
]
];
}
```

The above will receive an array like:
```php
[
'client_industry' => 1,
'client_potential' => 100000
]
```
And the `ClientFilter` will receive it as:
```php
[
'industry' => 1,
'potential_volume' => 100000
]
```
Allowing for more descriptive input names without filters needing to match. Allowing for more reuse of the same filters.

#### Filter Related Models With Both Methods
You can even use both together and it will produce the same result and only query the related model once. An example would be:

Expand Down
14 changes: 13 additions & 1 deletion src/ModelFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -429,7 +429,19 @@ public function filterUnjoinedRelation($related)
*/
public function getRelatedFilterInput($related)
{
return array_key_exists($related, $this->relations) ? Arr::only($this->input, $this->relations[$related]) : [];
$output = [];

if (array_key_exists($related, $this->relations)) {
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;
}
}
}

return $output;
}

/**
Expand Down
12 changes: 12 additions & 0 deletions tests/ModelFilterChildTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,18 @@ public function testRelationDotNotation()
$this->assertEquals(1, $users->count());
}

public function testFilterRelationsArrayAliases()
{
// client_name is defined as ['clients' => ['client_name' => 'name' ]];
// This will forward and call ClientFilter::name
$users = $this->model->filter(['client_name' => 'one'])->get();
$this->assertEquals(1, $users->count());

$client = new Client;
$clients = $client->filter(['owner_name' => 'Client1'])->get();
$this->assertEquals(1, $clients->count());
}

public function testPaginationWorksOnBelongsToMany()
{
if (method_exists(\Illuminate\Database\Eloquent\Relations\Relation::class, 'macro')) {
Expand Down
2 changes: 1 addition & 1 deletion tests/classes/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class Client extends Model

public function agent()
{
return $this->belongsTo(User::class);
return $this->belongsTo(User::class, 'user_id');
}

public function modelFilter()
Expand Down
7 changes: 6 additions & 1 deletion tests/classes/ClientFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ class ClientFilter extends ModelFilter
* @var array
*/
public $relations = [
'user' => ['owner_name'],
'agent' => ['owner_name'],
];

public function name($name)
{
$this->where('name', '=', $name);
}
}
7 changes: 6 additions & 1 deletion tests/classes/UserFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class UserFilter extends ModelFilter
* @var array
*/
public $relations = [
'clients' => ['client_name'],
'clients' => ['client_name' => 'name'],
];

public function setup()
Expand All @@ -30,4 +30,9 @@ public function clientLocation($location)
{
$this->related('clients.locations', 'name', $location);
}

public function ownerName($name)
{
$this->where('name', '=', $name);
}
}

0 comments on commit 02055b3

Please sign in to comment.