Download the PHP package camilo-manrique/laravel-filter without Composer

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

Laravel Eloquent Model Filter

Latest Stable Version License Build Status

This package allows querying your Eloquent models, based on URL queries. Using some simple rules, you can even filter based on related models and use diferent SQL comparison operators.

Instalation

Require package with composer:

Add CamiloManrique\LaravelFilter to your service providers on your config/app.php:

You can publish the configuration file to change the default settings:

Depending on your database column names and personal preferences, you might need change some of the defaults.

Usage

This package add two macro methods to the Builder class which allow to use the filtering methods without any further setup. Both methods can receive an Http Request, a Collection or an array as argument. The two methods are explained below.

Note on method names: The macros method names can be customized on the configuration file, in case there are naming conflicts on the Builder class macros. The default names will be used on the examples.

Get a query builder instance:

With this method you get a query builder instance, in which you can keep applying query builder methods, including get(), first(), paginate() and many others.

Get a model instance:

This method handles the query building and fetching for you. It even handles pagination for you out of the box. For default, automatic pagination using this method is turned on, but you can change this behavior publishing the configuration file and editing it.

Those methods can also be called without arguments, in which case no filters are applied to the query

Since those methods are macros of the Builder class, it can also chained when using related models on a model instance. For example, assuming that the model User has a relationship with Post model, you can use it like this:

Practical usage example

Returning from a route:

Returning from a controller:

A note on Eloquent API Resources: If you are using Laravel 5.5, you can also use this package with Eloquent Resources new feature:

Filtering rules

Now, this is an important section. We have explained how to install and call the filter methods, but how can you actually define your filters? Well, it's rather simple for basic queries and a little more verbose if you need to query based on related models.

Defining the filtering columns

In your request, you simply use the column names as keys and the comparison values as, well the values.

Example

Let's assume that your app URL is http://www.example.com and you defined a route /users that points to a controller method filter like this:

Let's say you want to retrieve the users from Germany. Your Http Request object should have a key named country with its value set to Germany. The URI request would be like this:

Now let's say you want to be more specific and you want to retrieve the users who, not only are from Germany, but also are males. Your URI would turn into something like this:

Remember that you can also use Collections and array to pass the desired filters. The equivalent of the last example using an array as parameter would be like this:

Query comparison operators

The above examples work only with exact matches, but you would probably need a more loose comparison like the one that >, <, LIKE and != operators offer. In order to use this operators, you append a keyword at the end of the column name, separated by a '/' character. This separation character can be changed on the configuration file.

This is the list of the keywords and their corresponding operators:

Example

Retrieve the users from Germany and under 30 years:

In the previous example, %2F is the encoding for the '/' character.

Equivalent form using an array as parameter:

Advanced Usage

Appending related models to the response

Sometimes you might need to fetch additional related models from your query. You can achieve this adding the keyword "relationships" to your input and setting its value to a comma-separated list of relationships (as defined in your model class, not the table name) you want to include.

If you have a column named "relationships" in your model, the filter will behave unexpectedly. In that case, change search in the config file for the relationship key on the keyword array and change its value to any other word you want that won't cause conflicts with your column names.

For example, let's say you have an User model with two related models, Posts and Comments, related model in a one to many relationship (a User can have many Posts and Comments). Now, you want to get only the users from Germany, but you want to include the posts and comments from the users in the response. The input would look like this:

This way, the Posts and Comments models from each user will be included in the response.

Be careful when loading relationships with many models, because all of them will be loaded, and can lead to very slow response times.

Filtering based on related models

Just as you can include related models on your result, you can also filter your results based on related models. You need only to prepend the relationship and a "@" character to the column name. For example, let's say a User model has a one on one relationship named "account_info", with a model named AccountInfo, and this model has an attribute called "name". To filter based the user model on the column "name" from the AccountInfo model, the input would be:

Get sum aggregate

Sometimes, you don't need the actual models from a query. Instead you might need the total sum of one or more attributes. In that case, you should add the keyword "sum" to your input, and setting its value to the column or columns you want to get the total sum (the columns should be comma-separated). An example would be getting the total votes from some user posts:

Another option is using the Eloquent relationships method:


All versions of laravel-filter with dependencies

PHP Build Version
Package Version
Requires php Version >=7.0.0
illuminate/config Version 5.*
illuminate/support Version 5.*
illuminate/database Version 5.*
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 camilo-manrique/laravel-filter contains the following files

Loading the files please wait ....