Download the PHP package coliving/eloquent-filter without Composer

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

Eloquent Filter Tweet

Latest Stable Version Run tests License GitHub stars StyleCI Build Status Monthly Downloads

Eloquent Filter adds custom filters to your Eloquent Models in Laravel. It's easy to use and fully dynamic.

Bitcoin Donate Button

Table of Content

Requirements

:microphone: Introduction

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

alt text

A simple implementation without Eloquent Filter

The Request URI could look like this:

http://localhost:8000/users/index?age_more_than=25&gender=male&created_at=25-09-2019

And a simple implementation in the Controller would look like this:

This solution is simple ,but that works fine. But you'd have to add a condition for each filter you need. Especially with more complex filtering, your code can become a Monster very fast! :boom:

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 will save you time and minimize the complexity of your code.

After installing Eloquent Filter the request URI could look like this:

http://localhost:8000/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'd just need that one line:

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

:electric_plug: Installation

1- Run this Composer command to install the latest version

  $ composer require coliving/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 your models and set fields that you will want to filter in the whitelist array. as well You can override this method in your models.

You can set * char for that filter in all fields aslike below example:

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

Set array to WhiteListFilter

Add new field to WhiteListFilter

Use in Controller

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 array to 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.

Call ignoreRequest that will ignore some requests that you don't want to use in conditions eloquent filter. For example, the perpage param will never be in the conditions eloquent filter. it's related to the paginate method. page param ignore by default in Eloquent Filter of Laravel.

Call AcceptRequest will accept requests which you want to use in conditions Eloquent Filter. For example username and id param will be in the conditions eloquent filter. Just notice you must set $whiteListFilter in Model. This method is useful for query string manipulation by a user.

Simple Examples

You just pass data blade form to query string or generate query string in the Controller method. For example:

Simple Where

Where In

This example make method whereIn.

OrWhere (New feature :fire:)

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 relation Model (New feature :fire:)

You can set all nested relation in the query string just by the array of query string. For example, 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. Just be careful you must set 'posts.count_post' and 'posts.orders.name' in the User model.

Where array the nested relation Model

You can pass array to make whereIn condition.

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 of 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 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:

Custom Detection Condition

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

You can make conditions to generate a new query after checking by that. (New feature :fire: ). 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 Also 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. for example:

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

Configuring

You can generate a config file to configure Eloquent Filter.

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]

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.

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.

Response Filter

Response Filter is a magic method for just changing response after handling by Eloquent Filter. The method called ResponseFilter You should implement the method ResponseFilter in your Model. For example


All versions of eloquent-filter with dependencies

PHP Build Version
Package Version
Requires php Version ^7.3|^8.0|^8.1
illuminate/support Version 5.8.*|~6.0|~7.0|~8.0|~9.0
illuminate/container Version 5.8.*|~6.0|~7.0|~8.0|~9.0
illuminate/database Version 5.8.*|~6.20.14|~7.0|~8.0|~9.0
illuminate/pagination Version 5.8.*|~6.0|~7.0|~8.0|~9.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 coliving/eloquent-filter contains the following files

Loading the files please wait ....