Download the PHP package eloquent-filter/filter without Composer

On this page you can find all versions of the php package eloquent-filter/filter. 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 filter

Eloquent Filter Tweet

Latest Stable Version Run tests License GitHub stars Monthly Downloads Scrutinizer Code Quality codecov

Eloquent Filter is a robust Laravel package providing an intuitive way to filter your Eloquent models using query strings. Perfect for crafting responsive APIs and complex data sets, this package seamlessly integrates with Laravel's existing Eloquent models, adding powerful and dynamic filtering capabilities with minimal setup.

Features:

We've tailored Eloquent Filter to be as flexible as you need—whether your queries are straightforward or complex. With an extensive feature set, you can implement specific functionalities unique to your application with ease.

Note We considered what predictable features you wanted to implement no matter simple or complex, although we have a lot of features to make able you to implement your specific something's else.

Table of Content

Requirements

Version information

The Eloquent Filter has reached more than 100,000 installations recently, so I made a decision to release version 4.0 with 2 new features.

The current version supports:

Major Version Versions Status PHP Version Laravel Version
^4.0 4.2.0 - 4.x.x Active support >= 8.2 >= 11.x
^4.0 4.0.x - 4.1.5 Active support >= 8.0 >= 9.x - <= 10.x
^3.0 3.2.x - 3.4.x End of life >= 8.0 >= 9.x
^3.0 3.0.0 - 3.0.5 End of life >= 7.4.0 >= 5.6.x - <= 8.x
^2.0 2.0.0 - 2.6.7 End of life <= 7.4.0 >= 5.x - <= 5.4

:microphone: Introduction

Conceivably, you would face challenges if you've done a task as an end-point in which there are some queries with many advanced options.

Let's say we want to make an advanced search page with multiple filter options.

A simple implementation without Eloquent Filter

The Resource URI would be look like:

/users/index?age_more_than=25&gender=male&created_at=25-09-2019

And a simple implementation in the Controller would look like :

This solution is simple and that works well but not for an enterprise project. But you'd have to add a condition for each filter you need. Especially if you would make more complex filtering, your code can become a monster quickly! :boom:

Hence, Eloquent Filter is ready for to you get rid of complexity in addition to saving time.

A simple implementation with Eloquent Filter

Eloquent Filter can help you to fix that ``problem. Just you will set query string to work with that. It would make your own query automatically and systematically while you can control them.

Right After installing Eloquent Filter, the request URI would be like this:

/users/list?age_more_than[operator]=>&age[value]=35&gender=male&created_at[operator]==>&created_at[value]=25-09-2019

And in the Controller, You just need that one line:

By Eloquent filter implementation, you can use all the documented filters!

:electric_plug: Installation

1- Run the following command in your project directory to add the Eloquent Filter as a dependency

    $ composer require mehdi-fathi/eloquent-filter

2- Add eloquentFilter\ServiceProvider::class to provider app.php

3- Add Facade 'EloquentFilter' => eloquentFilter\Facade\EloquentFilter::class to aliases app.php

That's it enjoy! :boom:

:book: Basic Usage

Config Model and set whitelist

Add the Filterable trait to yourself models and set fields in the whitelist array in which you will want to use of filter . You can override this method in your models as well.

You able add or set $whiteListFilter on the fly in your method. For example:

Set array to WhiteListFilter

Add new field to WhiteListFilter

-Note Just in case, you must set $whiteListFilter in Models. Aim of the method avert to manipulation query string by a bad user.

Conditions Guidance Table

Condition Name Eloquent Method Param Example Eloquent DB
WhereCustomCondition Declared custom
method of Model
SpecialCondition f_params[limit]=10 support f_params, e.g:
limit and order
WhereBetweenCondition whereBetween created_at[start]=2016/05/01
&created_at[end]=2017/10/01
whereBetween(
'created_at',
[{start},{end}])
WhereByOptCondition where count_posts[operator]=>&
count_posts[value]=35
where('column',
">", $value)
WhereLikeCondition where first_name[like]=John where('column',
'like', $value)
WhereInCondition whereIn username[]=David&
username[]=John12
whereIn('column', $value)
WhereOrCondition orWhere username=Bill&
or[username]=James
orWhere('column', $value)
WhereHas WhereHas posts[title]=sport one whereHas('posts',
function ($q)
{$q->where('title', $value)});
WhereDoesntHaveCondition whereDoesntHave doesnt_have=category doesntHave($value)
WhereDateCondition whereDate created_at=2024-09-01 whereDate('column', $value)
WhereCondition where username=Mehdi where('column', $value)

Simple Examples

You just pass data form as query string. For example:

Simple Where

Where In

This example make method whereIn.

OrWhere

This example make method orWhere().

Where like

If you are going to make a query by like conditions. You can do that by this example.

Where by operator

You can set any operator mysql in the queries string.

Where the nested relations Model

You can set all nested relations in the query string just via the array of query string. imagine, the user model has a relation with posts. And posts table has a relation with orders table.

You can make query conditions by set posts[count_post] and posts[orders][name] in the query string.

Where array the nested relation Model

You can pass array to make whereIn condition.

Doesnthave Where (new feature)

Special Params

You can set special params limit and orderBy in the query string to make a query by that.

Where between

If you are going to make a query based on date, You must fill keys, start, and end in the query string. Hence You can set it as a query string. These params are used for the filter by date.

Advanced Where

Therefore, fields of query string are same rows table database in $whiteListFilter in your model or declare the method in your model as override method. The overridden method can be considered a custom query filter.

Custom Query Filter

Eloquent Filter doesn't support all the conditions by default. For this situation, you can make an overridden method. If you are going to make yourself a query filter, you can do it easily. You should take care of using filterCustom before method name in new version.

You should run the command to make a trait and use it on the model:

php artisan eloquentFilter:filter users

-Note These fields of query string are the same methods of the trait. Use trait in your model:

Some Filter methods

Request encoded

In particular projects, We don't want to share our request filters with all users. It means every single user should have a unique valid url for duplicated search then It works just for the same user.

You just need to pass hashed_filters as key to detect hashed_filter and encode them in the core. Every single user has a unique url per user. In addition, nobody can manipulate parameters in order to get specific data.

For this purpose, You had better have an end-point to set and get request encoded then pass it to your main end-point for result.

Custom Detection Conditions

Sometimes you want to make your custom condition to make a new query that Eloquent Filter doesn't support by default. The good news is you would make a custom condition in the eloquent filter from now on.

You can make conditions to generate a new query after checking by that. For example:

We must have two classes. The First detects conditions second class generates the query.

Make method EloquentFilterCustomDetection in the above example and return array conditions class.

You just run code User::filter(); for see result.

-Note as well, you can set custom detection on the fly by use of the method SetCustomDetection. For example :

-Note You can disable EloquentFilterCustomDetection on the fly by this code :

-Note You can set many detection conditions. e.g:

-Note Every custom detection will run before any detections by default eloquent filter.

Configuring

You can publish the configuration file to customize the package further:

Publish Config

php artisan vendor:publish --provider="eloquentFilter\ServiceProvider"

Config

For example, if you set 'request_filter_key' => 'filter', that Eloquent Filter recognizes filter query string.

/users/list?filter[email][email protected]

It's disable by default you enable by has_keeping_query, type is type log ,and max_time_query is a value for keeping queries with high time-executed.

Alias

Sometimes you may want to change some parameters in the URL while those mention a field of the model. e.g. name of the input form is not similar to the model ,or you want to change them for other reasons so the alias as a new feature can be useful.

Then you should send the code param in the URL for making a query with the national code field of the model readily.

Query Builder Introduction

Great news!

Some people asked me a lot to add new feature to support Laravel query builder. It needed a lot of energy and devoting time , so I decided to implement it. It's quite tough however finally it's almost done now.

We are supporting query builder along with eloquent from now on. Not only you would use query builder ,but also you can use eloquent at the same time.

It's a new feature ,and I'm snowed under the code to fix issues. Anyway this feature is up right now with just some limitation. We don't support WhereCustomCondition and WhereHas for query builder at the moment but other conditions were ready to use. in addition, we don't have any kind of whitelist , blacklist , custom detectioon or alias. currently , It's just a simple feature.

Magic Methods

Magic methods are a collection of methods that you can use as a wrapper in the Eloquent Filter. For example, serialize data before filtering or changing data in response and others. Now Eloquent Filter have serializeRequestFilter,ResponseFilter and , etc.

Request Methods

Call ignoreRequest (static scope) or ignoreRequestFilter will ignore some requests that you don't want to use in conditions of eloquent filter.

Change your code the controller of the laravel project as like below example:

-Note The Eloquent Filter config by default uses the query string to make queries in Laravel. Although, you can set the collection data in the filter method Model for making your own custom condition without query string.

-Note Therefore you must unset yourself param as perpage. Just you can set page param for paginate this param ignore from the filter.

e.g: the perpage param will never be in the conditions eloquent filter. It has to do with to the paginate method. page param ignore by default in Eloquent Filter of Laravel.

Calling AcceptRequest (static scope) or acceptRequestFilter will accept requests in which you want to use in conditions Eloquent Filter. e.g: username and id key will be in the conditions eloquent filter.

Request Filter

Eloquent Filter has a magic method for just change requests injected before handling by eloquent filter. This method is SerializeRequestFilter. You just implement SerializeRequestFilter method in your Model. For example

As above code, you can modify every query params of the Model in the method serializeRequestFilter before running by Eloquent Filter. That is a practical method when you want to set user_id or convert date or remove space and others.

Request Field Cast Filter

Eloquent Filter requires a bunch of specific methods for each of the fields before going on filter process. This feature has been implemented recently. By this filterSet + field method in your model, You will be able to add some change for that particular field.

Response Filter

Response Filter is an overriding method for changing response right after handle by Eloquent Filter. The method called getResponseFilter and You could implement the method getResponseFilter in your Model. e.g:

Black List Detections

Obviously, you never want all users who are able to get data by manipulating requests. As a result, we'd better have an eloquent control feature. Although we have this ability on request side, we need this feature on Eloquent side as well.

We would set a blacklist detection to prevent making conditions by using it. Therefore, that list has been disabled in making conditions. for example:

Macro Methods

-isUsedEloquentFilter is a macro method for builder/DB to check either query used eloquent-filter.

-getDetectionsInjected is a macro method to get list array of injected objects.

e.g:

Contributing

If you'd like to contribute to Eloquent Filter, please fork the repository and create a pull request. We welcome contributions of all kinds, including bug fixes, new features, and documentation improvements.

Proposed Features (Under Consideration)

We are constantly working to improve our package and have planned the following features for upcoming releases:

Your contributions are always welcome! If you would like to help with the development of these features.

License

Eloquent Filter is open-source software licensed under the MIT license.

Contact

If you have any questions or feedback about Eloquent Filter, please feel free to contact us at [email protected]. We'd love to hear from you!

Acknowledgements

We'd like to thank the Laravel community for their support and contributions to this project.


All versions of filter with dependencies

PHP Build Version
Package Version
Requires php Version ^8.0|^8.1|^8.2|^8.3
illuminate/support Version ~8.0|~9.0|~10.0|~11.0
illuminate/container Version ~8.0|~9.0|~10.0|~11.0
illuminate/database Version ~8.0|~9.0|~10.0|~11.0
illuminate/pagination Version ~8.0|~9.0|~10.0|~11.0
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 eloquent-filter/filter contains the following files

Loading the files please wait ....