LaravelPackages.net
Acme Inc.
Toggle sidebar
aporat/laravel-filter-var

A Laravel package for filtering and sanitizing request variables with customizable rules

2.067
2
v5.0.0
About aporat/laravel-filter-var

aporat/laravel-filter-var is a Laravel package for a laravel package for filtering and sanitizing request variables with customizable rules. It currently has 2 GitHub stars and 2.067 downloads on Packagist (latest version v5.0.0). Install it with composer require aporat/laravel-filter-var. Discover more Laravel packages by aporat or browse all Laravel packages to compare alternatives.

Last updated

Laravel Filter Var

Latest Stable Version Downloads Codecov Laravel Version GitHub Actions Workflow Status License

A Laravel package for filtering and sanitizing request variables with a chainable, customizable filter system.

Features

  • Chain multiple filters (e.g., trim, uppercase, cast) in a single call.
  • Built-in filters for common tasks (e.g., strip_tags, escape, format_date).
  • Support for custom filters via configuration.
  • Seamless integration with Laravel's service container and facade system.

Requirements

  • PHP: 8.4 or higher
  • Laravel: 12.x or 13.x
  • Composer: Required for installation

Installation

Install the package via Composer:

composer require aporat/laravel-filter-var

The service provider (FilterVarServiceProvider) is automatically registered via Laravel’s package discovery. If you’ve disabled auto-discovery, add it manually to config/app.php:

'providers' => [
    // ...
    \Aporat\FilterVar\FilterVarServiceProvider::class,
],

Optionally, register the facade for cleaner syntax:

'aliases' => [
    // ...
    'FilterVar' => Aporat\FilterVar\Facades\FilterVar::class,
],

Publish the configuration file to customize filters:

php artisan vendor:publish --provider="Aporat\FilterVar\FilterVarServiceProvider" --tag="config"

This copies config/filter-var.php to your Laravel config directory.

Usage

Basic Filtering

Filter and sanitize a request variable using the facade:

use Aporat\FilterVar\Facades\FilterVar;

$userAgent = FilterVar::filterValue('cast:string|trim|strip_tags|escape', $request->header('User-Agent'));

This:

  • Casts the input to a string.
  • Trims whitespace.
  • Removes HTML/PHP tags.
  • Escapes special HTML characters.

Available Filters

| Filter | Description | Example Input | Example Output | |------------------------|----------------------------------------------------|---------------------------|------------------------| | capitalize | Capitalizes words (title case) | hello world | Hello World | | cast:<type> | Casts to a type (e.g., int, string, bool, array, object, collection) | 123.45 (cast:int) | 123 | | digit | Extracts digits only | abc123xyz | 123 | | escape | Escapes HTML special characters | <p>Hello &</p> | &lt;p&gt;Hello &amp;&lt;/p&gt; | | filter_if | Conditional check on array key/value | ['key' => 'val'] | true/false | | format_date | Reformats a date string | 2023-01-15 | 15/01/2023 | | lowercase | Converts to lowercase | HELLO | hello | | normal_string | Strips tags and keeps A-Z, 0-9, space, -:_. | <script>alert(1)</script> | alert1 | | remove_whitespace | Removes all whitespace | a b c | abc | | slugify | Converts string into URL-friendly slug | Hello World! | hello-world | | strip_tags | Removes HTML/PHP tags | <b>Hello</b> | Hello | | trim | Trims whitespace | hello | hello | | uppercase | Converts to uppercase | hello | HELLO |

Chaining Filters

Chain multiple filters using the | separator:

$result = FilterVar::filterValue('trim|uppercase|cast:string', '  hello world  ');
// Returns: "HELLO WORLD"

Custom Filters

Add custom filters by editing config/filter-var.php:

return [
    'custom_filters' => [
        'media_real_id' => \App\Filters\MediaRealId::class,
    ],
];

Define the custom filter class:

namespace App\Filters;

use Aporat\FilterVar\Contracts\Filter;

class MediaRealId implements Filter
{
    public function apply(mixed $value, array $options = []): string
    {
        $value = (string) $value;
        return str_contains($value, '_') ? explode('_', $value, 2)[0] : $value;
    }
}

Use it:

$result = FilterVar::filterValue('media_real_id', '11111_22222');
// Returns: "11111"

Using Without Facade

Resolve from the container:

$result = app('filter-var')->filterValue('trim', '  hello  ');
// Returns: "hello"

Testing

Run the test suite:

composer test

Generate coverage reports for CI:

composer test-ci

Contributing

Contributions are welcome! Please:

  1. Fork the repository.
  2. Create a feature branch (git checkout -b feature/amazing-feature).
  3. Commit your changes (git commit -m "Add amazing feature").
  4. Push to the branch (git push origin feature/amazing-feature).
  5. Open a pull request.

See CONTRIBUTING.md for details.

License

This package is open-sourced under the MIT License. See the License File for more information.

Support

Star History Chart