Download the PHP package skover/eloquent-builder without Composer
On this page you can find all versions of the php package skover/eloquent-builder. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package eloquent-builder
Provides a Eloquent query builder for Laravel 5
This package allows you to build eloquent queries, based on request parameters. It greatly reduces the complexity of the queries and conditions, which will make your code cleaner.
Installation
Laravel 5.5 uses Package Auto-Discovery, so you are not required to add ServiceProvider manually.
Laravel <= 5.4.x
If you don't use Auto-Discovery, add the ServiceProvider to the providers array in config/app.php
file
And add the facade to your config/app.php
file
Default Filters Namespace
The default namespace for all filters is App\EloquentFilters\
with the base name of the Model.
For example:
Suppose we have a User model with an AgeMoreThan filter.As a result, the namespace filter must be as follows:
App\EloquentFilters\User\AgeMoreThanFilter
With Config file
You can optionally publish the config file with:
And set the namespace for your model filters which will reside in:
Usage
Suppose we want to get the list of the users with the requested parameters as follows:
In the legacy code the method written below was followed:
But the new method with EloquentBuilder follows the steps below:
Note: It's recommended validates the incoming requests before sending to filters.
Define a Filter
Writing a filter is simple. Define a class that implements the Fouladgar\EloquentBuilder\Support\Foundation\Contracts\IFilter
interface. This interface requires you to implement one method: apply
. The apply
method may add where constraints to the query as needed:
Ignore Filters on null value
Filter parameters are ignored if contain empty values or null.
Suppose we have a request something like:
Only the "published_post" filter will be applied on your query.
Work with existing queries
You may also want to work with existing queries. For example, consider the following code:
Use as Dependency Injection
Suppose you want use the EloquentBuilder
as DependencyInjection
in a Repository
.
Let's have an example.We have a sample UserRepository
as follows:
The filters
method applies the requested filters to the query by using EloquentBuilder
injected.
Injecting The Repository
Now,we can simply "type-hint" it in the constructor of our UserController
:
Testing
Contributing
Please see CONTRIBUTING for details.
Security
If you discover any security related issues, please email [email protected] instead of using the issue tracker.
License
Eloquent-Builder is released under the MIT License. See the bundled LICENSE file for details.
Built with :heart: for you.