Download the PHP package meius/laravel-filter without Composer
On this page you can find all versions of the php package meius/laravel-filter. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package laravel-filter
Laravel Filter Package
Table of Contents
- Overview
- Requirements
- Getting Started
- Installation
- Configuration
- Usage
- Creating Filters
- Applying Filters
- Applying Filters to Route Groups
- Caching Filters
- Example
- Using
ExcludeFrom
andOnlyFor
Attributes - Prioritization of Settings
- Example Request Structure
- Advanced Usage
- Configuring Filter Aliases and Prefixes
- Adding Filter Aliases to Models
- Configuring the Filter Prefix
- Example JSON and URL Requests
- Examples for Other Databases
- Support
- License
Overview
The meius/laravel-filter
package provides a convenient way to apply filters to Eloquent models in a Laravel application. It allows you to define filters using attributes and apply them dynamically based on the request.
Requirements
- PHP >= 8.0
- Laravel >= 9.0
Getting Started
To get started with the meius/laravel-filter
package, follow the installation instructions below and check out the usage examples.
Installation
- Install the package via Composer:
Configuration
- To publish the configuration file, run the following command:
This will create a config/filter.php
file where you can customize the filter settings.
Usage
Creating Filters
- To create a new filter, use the
make:filter
Artisan command:
Applying Filters
-
Define filters using attributes in your controller methods:
- To apply filters to related models, use the
ApplyFiltersTo
attribute with multiple model classes:
Applying Filters to Route Groups
By default, filters are applied to the web and API route groups. To customize this behavior, you must publish the configuration file and make the necessary adjustments.
-
Publish the Configuration File:
- Update the Configuration:
Open the
config/filter.php
file and modify theroute_groups
section to specify which groups should have filters applied.
Caching Filters
- To cache the filters for faster loading, run the following Artisan command:
Example
Here is an example of how to define and apply filters:
-
Create a filter:
-
Create a filter for related models:
- If you need to apply a filter according to a condition, you can use the
canContinue
method:
Using ExcludeFrom
and OnlyFor
Attributes
You can use the ExcludeFrom
and OnlyFor
attributes to conditionally apply filters.
-
Create a filter with
ExcludeFrom
: -
Create a filter with
OnlyFor
: -
You can also use the
ExcludeFrom
andOnlyFor
attributes together(If you need it \@_@/): -
Create a filter using properties:
- Create a filter using methods:
Prioritization of Settings
When defining filter settings, the priority of the settings is as follows:
- Method: The highest priority. If a method is defined to specify the filter settings, it will override any settings defined via properties or attributes.
- Property: Medium priority. If a property is defined to specify the filter settings, it will override any settings defined via attributes but will be overridden by method definitions.
- Attribute: The lowest priority. If settings are defined via attributes, they will be used only if there are no corresponding settings defined via methods or properties.
This means that if there are conflicting settings defined in multiple ways, only the setting with the highest priority will be applied. For example, if a filter has both a method and an attribute defining the same setting, the method's setting will take precedence and be applied, while the attribute's setting will be ignored.
Example Request Structure
-
For filters to work correctly, the query must have the appropriate structure. Here is an example of how the query should be structured:
- Example request:
Configuring Filter Aliases and Prefixes
Adding Filter Aliases to Models
- To add filter aliases to your models, use the
HasFilterAlias
trait and define the$filterAlias
property in your model.
Configuring the Filter Prefix
- You can change the filter prefix by modifying the
config/filter.php
configuration file. Update theprefix
value to your desired string.
Example JSON and URL Requests
-
JSON Request
Here,
"f"
is the filter prefix specified in the config, and"h5p"
is the alias defined in the model. -
URL Request
Similarly, in the URL request,
"f"
represents the filter prefix and"h5p"
is the alias.
Advanced Usage
Complex Filters
- Create a complex filter that combines multiple conditions:
Combined Filters
- Apply multiple filters to a single query:
Examples for Other Databases
Using the query
method, you can create filters for different databases.
PostgreSQL
- Create a filter for a PostgreSQL database:
SQLite
- Create a filter for an SQLite database:
Support
For support, please open an issue on the GitHub repository.
Contributing
We welcome contributions to the meius/laravel-filter
library. To contribute, follow these steps:
- Fork the Repository: Fork the repository on GitHub and clone it to your local machine.
- Create a Branch: Create a new branch for your feature or bugfix.
- Write Tests: Write tests to cover your changes.
- Run Tests: Ensure all tests pass by running
phpunit
. - Submit a Pull Request: Submit a pull request with a clear description of your changes.
For more details, refer to the CONTRIBUTING.md file.
License
This package is open-sourced software licensed under the MIT license.
All versions of laravel-filter with dependencies
illuminate/console Version ^9.0 || ^10.0 || ^11.0 || ^12.0
illuminate/database Version ^9.0 || ^10.0 || ^11.0 || ^12.0
illuminate/filesystem Version ^9.0 || ^10.0 || ^11.0 || ^12.0
illuminate/http Version ^9.0 || ^10.0 || ^11.0 || ^v12.0
illuminate/routing Version ^9.0 || ^10.0 || ^11.0 || ^12.0
illuminate/support Version ^9.0 || ^10.0 || ^11.0 || ^12.0