Skip to content

Commit

Permalink
Merge pull request #1 from CodeWithDennis/custom-column-usage
Browse files Browse the repository at this point in the history
Update PriceFilter to allow custom column selection
  • Loading branch information
CodeWithDennis authored Oct 16, 2024
2 parents 7048aa8 + 6155ff6 commit 4b52731
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 4 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,19 @@ This is the contents of the published config file:
return [
'currency' => 'USD',
'cents' => true,
'column' => 'price'
];
```

## Usage

By default, the column that the filter will use is `price`, but you can change it to any column you want.

```php
PriceFilter::make()
->currency(column: 'total_price')
```

By default, the currency is set to USD globally, but you can change it per filter to any currency you want.

```php
Expand Down
1 change: 1 addition & 0 deletions config/filament-price-filter.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@
return [
'currency' => 'USD',
'cents' => true,
'column' => 'price',
];
20 changes: 16 additions & 4 deletions src/Filament/Tables/Filters/PriceFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ class PriceFilter extends Filter
{
public Closure | string | null $currency = null;

public Closure | string | null $column = null;

public Closure | string | null $locale = null;

public Closure | bool | null $cents = null;
Expand All @@ -21,11 +23,12 @@ public static function getDefaultName(): ?string
return 'priceFilter';
}

public function currency(Closure | string | null $currency = null, Closure | string | null $locale = null, Closure | bool $cents = true): static
public function currency(Closure | string | null $currency = null, Closure | string | null $locale = null, Closure | bool $cents = true, Closure | string | null $column = null): static
{
$this->currency = $currency;
$this->locale = $locale;
$this->cents = $cents;
$this->column = $column;

return $this;
}
Expand All @@ -48,6 +51,15 @@ public function getLocale(): string
return $this->evaluate($this->locale);
}

public function getColumn(): string
{
if ($this->column === null) {
return config('filament-price-filter.column');
}

return $this->evaluate($this->column);
}

public function getCents(): bool
{
if ($this->cents === null) {
Expand Down Expand Up @@ -99,12 +111,12 @@ protected function setUp(): void

return $query
->when($data['from'] !== null, function (Builder $query) use ($cents, $data) {
return $query->where('price', '>=', $cents ? (float) $data['from'] * 100 : (float) $data['from']);
return $query->where($this->getColumn(), '>=', $cents ? (float) $data['from'] * 100 : (float) $data['from']);
})
->when($data['to'] !== null, function (Builder $query) use ($data, $cents) {
return $data['to'] > 0
? $query->where('price', '<=', $cents ? (float) $data['to'] * 100 : (float) $data['to'])
: $query->where('price', 0);
? $query->where($this->getColumn(), '<=', $cents ? (float) $data['to'] * 100 : (float) $data['to'])
: $query->where($this->getColumn(), 0);
});
});

Expand Down

0 comments on commit 4b52731

Please sign in to comment.