Download the PHP package jerome/filterable without Composer

On this page you can find all versions of the php package jerome/filterable. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package filterable

Filterable

About Filterable

Latest Version on Packagist Tests Check & fix styling Total Downloads

The Filter class provides a flexible and powerful way to apply dynamic filters to Laravel's Eloquent queries. It supports caching, user-specific filtering, and custom filter methods, making it suitable for a wide range of applications, from simple blogs to complex data-driven platforms.

Features

Installation

To integrate the Filterable package into your Laravel project, you can install it via Composer. Run the following command in your project directory:

Upon installation, the package should automatically register its service provider with Laravel's service container, making its features readily available throughout your application. This leverages Laravel's package auto-discovery mechanism, which is supported in Laravel 5.5 and later versions.

If you are using a version of Laravel that does not support package auto-discovery, you will need to manually register the FilterableServiceProvider in your config/app.php file, under the providers array:

This step is typically not necessary for modern Laravel installations, as auto-discovery should handle it for you.

After installation and registration, you're ready to use the Filterable features to enhance your Laravel application's data querying capabilities.

Usage

Creating a Filter Class

You can create a new filter class using the following Artisan command:

This command will generate a new filter class in the app/Filters directory. You can then customise this class to add your own filter methods.

The Filter class is a base class that provides the core functionality for applying filters to Eloquent queries. You can extend this class to create your own filter classes tailored to your specific models. To use the Filter class, you first need to extend it to create your own filter class tailored to your specific model. Here's a basic example for a Post model:

To add a new filter, simply define a new method within your custom filter class. This method should adhere to PHP's camelCase naming convention and be named descriptively based on the filter's purpose. Once you've implemented the method, ensure to register its name in the $filters array to activate it. Here's how you can do it:

In this example, a new filter lastPublishedAt is created in the PostFilter class. The filter name last_published_at is registered in the $filters array.

Implementing the Filterable Trait and Filterable Interface

To use the Filter class in your Eloquent models, you need to implement the Filterable interface and use the Filterable trait. Here's an example for a Post model:

Note: The Filterable interface and Filterable trait are included in the package and should be used in your models to enable filtering. The Filterable interface is optional but recommended for consistency.

Applying Filters

You can apply filters to your Eloquent queries like so:

Applying Filters in Controllers

You can apply your custom filters in your controller methods like so:

Applying Filters Scoped to the Authenticated User

You can also apply filters that are specific to the authenticated user. The forUser method sets the user for which the filters should be applied:

Applying Pre-Filters to run before the main filters

You can also apply pre-filters that run before the main filters. The registerPreFilters method sets the pre-filters that should be applied:

Using Filters on the Frontend

You can use filters on the frontend by sending a request with query parameters. For example, to filter posts by status, you can send a request like this:

This request will return all posts with the status active.

You can also string together all the filters you want to apply. For example, to filter posts by status and category, you can send a request like this:

This request will return all posts with the status active and associated with the category of ID 2.

Note: Any query parameters that do not match the filter names will be ignored.

Caching

In your filter class, you can control caching by using the enableCaching static method. Set the $useCache static property to true to enable caching, or false to disable it. You can also customise the duration of the cache by modifying the $cacheExpiration property.`

Caching is disabled by default.

Enabling and Disabling Caching

This configuration allows you to manage caching settings centrally from the AppServiceProvider. Adjusting caching behavior based on the environment or specific scenarios helps optimize performance and resource utilization effectively.

Certainly! Here’s a detailed usage guide section that explains how to utilize the logging functionality in the Filter class. This guide is designed to help developers understand and implement logging within the context of filtering operations effectively.

Logging

The Filter class incorporates robust logging capabilities to aid in debugging and monitoring the application of filters to query builders. This functionality is crucial for tracing issues, understanding filter impacts, and ensuring the system behaves as expected.

Configuring the Logger

  1. Setting Up Logger: Before you can log any activities, you must provide a logger instance to the Filter class. This logger should conform to the Psr\Log\LoggerInterface. Typically, this is set up in the constructor or through a setter method if the logger might change during the lifecycle of the application.

  2. Dependency Injection: If you are using Laravel, you can leverage its service container to automatically inject the logger into your Filter class.

Setting Up Logger with a Custom Channel

You can set up a specific logging channel for your Filter class either by configuring it directly in the logger setup or by defining it in Laravel’s logging configuration and then injecting it. Here’s how to do it in both ways:

  1. Direct Configuration:

    • Directly create a logger with a specific channel and handler. This method is straightforward and gives you full control over the logger’s configuration:
  2. Using Laravel's Logging Configuration:

    • Laravel allows you to define custom channels in its logging configuration file (config/logging.php). You can define a specific channel for the Filter class there and then retrieve it using the Log facade:

Enabling and Disabling Logging

Logging Actions

Checking If Logging Is Enabled

Testing

Testing your filters can be done using PHPUnit. Here’s an example test that ensures a status filter is applied correctly:

Ensure you have the necessary testing environment set up, including any required migrations or factory definitions.

License

This project is licensed under the MIT License - see the LICENSE.md file for details.

Contributing

Contributions are what make the open-source community such an amazing place to learn, inspire, and create. Any contributions you make are greatly appreciated.

If you have a suggestion that would make this better, please fork the repository and create a pull request. You can also simply open an issue with the tag "enhancement".

Don't forget to give the project a star! Thanks again!

  1. Fork the Project
  2. Create your Feature Branch (git checkout -b feature/amazing-feature)
  3. Commit your Changes (git commit -m 'Add some amazing-feature')
  4. Push to the Branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

Authors

See also the list of contributors who participated in this project.

Acknowledgments


All versions of filterable with dependencies

PHP Build Version
Package Version
Requires php Version ^8.2
illuminate/cache Version ^10.0|^11.0
illuminate/contracts Version ^10.0|^11.0
illuminate/database Version ^10.0|^11.0
illuminate/http Version ^10.0|^11.0
illuminate/support Version ^10.0|^11.0
nesbot/carbon Version ^2.72|^3.0
spatie/laravel-package-tools Version ^1.11
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package jerome/filterable contains the following files

Loading the files please wait ....