Download the PHP package mjahn/eloquent-builder without Composer
On this page you can find all versions of the php package mjahn/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 or Lumen
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.
Basic Usage
Suppose you 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 after using the EloquentBuilder, the above code refactor as follows:
You just need to define filter for each parameter that you want to add to the query.
Tip: It's recommended validates the incoming requests before sending to filters.
Tip: It's recommended present filters inside a filter key in query string like this:
user/search?filter[age_more_than]=25&filter[gender]=male
and then get them in this way:$request->only('filter')
.
Installation
- Laravel
- Lumen
Laravel
You can install the package via composer:
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
Lumen
You can install the package via composer:
For Lumen, add the LumenServiceProvider
to the bootstrap/app.php
file
For using the facade you have to uncomment the line $app->withFacades();
in the bootstrap/app.php
file
After uncommenting this line you have the EloquentBuilder
facade enabled
Publish the configuration file
and add the configuration to the bootstrap/app.php
file
Important : this needs to be before the registration of the service provider.
Default Filters Namespace
The default namespace for all filters is App\EloquentFilters
with the base name of the Model. For example, the filters namespace will be App\EloquentFilters\User
for the User
model.
With Config file
You can optionally publish the config file with:
And set the namespace for your model filters which will reside in:
Define a Filter
Writing a filter is simple. Define a class that extends
the Fouladgar\EloquentBuilder\Support\Foundation\Contracts\Filter
abstract class. This class requires you to implement one method: apply
. The apply
method may add where constraints to the query as needed.
Each filter class should be suffixed with the word Filter
.
For example, take a look at the filter defined below:
Tip: Also, you can easily use local scopes in your filter. Because they are instance of the query builder.
Authorizing Filter
The filter class also contains an authorize
method. Within this method, you may check if the authenticated user actually has the authority to apply a given filter. For example, you may determine if a user has a premium account, can apply the StatusFilter
to get listing the online or offline people:
By default, you do not need to implement the authorize
method and the filter applies to your query builder.
If the authorize
method returns false
, a HTTP response with a 403 status code will automatically be returned.
Ignore Filters on null value
Filter parameters are ignored if contain empty or null values.
Suppose you have a request something like this:
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.
All versions of eloquent-builder with dependencies
illuminate/database Version ~5.5.0|~5.6.0|~5.7.0|~5.8.0|~6.0
illuminate/http Version ~5.5.0|~5.6.0|~5.7.0|~5.8.0|~6.0
illuminate/support Version ~5.5.0|~5.6.0|~5.7.0|~5.8.0|~6.0