Download the PHP package backfron/laravel-finder without Composer
On this page you can find all versions of the php package backfron/laravel-finder. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download backfron/laravel-finder
More information about backfron/laravel-finder
Files in backfron/laravel-finder
Package laravel-finder
Short Description Build and handle complex Eloquent searchs easely.
License MIT
Homepage https://github.com/backfron/laravel-finder
Informations about the package laravel-finder
LaravelFinder
backfron/laravel-finder
provides an easy way to build complex database searchs in a scalable way using ELoquent. If you have a form with a bunch of fields by which you must filter your data, LaravelFinder maybe suit your needs.
These package was inspired by Amo Chohan's article.
Installation
Via Composer
Usage
Finders
Lets imagine that we need to build a search feature for tasks. You have a Task
model and want to perform a query by one or many conditions like: status, creation date, owner, finish date, etc. To To do so, you may create a Finder. A Finder is basicaly a class where we can define the model for which we will be building the search. LaravelFinder offers an artisan command for quickly scaffold Finders.
These command will create a new Finder located at app/Finders/Tasks/TaskFinder.php
. If you inspect the file you will see that is a very short class that basically stores the model with which we will build the query.
Filters
Next we need to create the Filters. Filters are where we will write the logic for our query. Each filter should handle a single field condition. For example, if we need to search a Task by their status, we just need to create a filter for that purpouse. LaravelFinder offers an artisan command for quickly scaffold your filters. You must specify the name of the Filter and the model to which it will be applied. The name of the filter idealy should match with the field name in your database table.
These command will create a file located at app/Finders/Tasks/Filters/Status.php
. These file contains a class and a method called apply
that receives an instance of the Eloquent query builder (\Illuminate\Database\Eloquent\Builder) and the value with which the search will be performed.
Of course, you can modify the query to fit your needs. For example:
You can create as many Filters as you need. So for example imagine we created the filters: Status, UserOwnerId and FinishedAt. Then in your controller you can receive a request that includes those fileds in snake_case version, and pass it to the filters
static method of your Finder.
Under the hood, LaravelFinder will take the field names and will call the appropriate Filter. For status
field the Status
filter will be called, for user_owner_id
field the UserOwnerId filter will be called, for the finished_at
field the FinishedAt
field will be called an so on.
After setting your filters, you can even call any method that you would normally call in your model instance, that means you can continue chaining Eloquent methods after applying your filters.
What happen if a field don't have a match Filter? No worries, by default LaravelFinder will ignore any field that don't match with any of the available filters. You can change these behavior by publishing the config file laravel-finder.php like these:
And once the config file is published at config/laravel-finder.php
, you may set to false
the ignore-unexisting-filters
key. When set to false
LaravelFinder will throw an Backfron\LaravelFinder\Exceptions\FilterNotFoundException
exception if you include a filter that don't exists yet.
You even can create your Finder and your filters in one command call.
Global filters
If you want to apply a global filter to your query, you can do it through the global
method and passing a Closure as parameter.
In case you want to use some of your created filters as a global filter you can do it like this:
TODO
- Include in the filter file blueprint a condition to avoid the application of the filter if the parameter is empty.
Change log
Please see the changelog for more information on what has changed recently.
Contributing
Please see contributing.md for details and a todolist.
Security
If you discover any security related issues, please email [email protected] instead of using the issue tracker.
Credits
- Jairo Ushiña
- All Contributors
License
Please see the license file for more information.