Download the PHP package matejsvajger/laravel-distillery without Composer

On this page you can find all versions of the php package matejsvajger/laravel-distillery. 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-distillery

Total Downloads License

Introduction

Laravel Distillery provides an elegant way for filtering and paginating Eloquent Models. Distillery taps into native Laravel API Resource Collections and builds a paginated filtered result of models/resources while making it possible to use the Laravel's pagination templates.

Installation & Configuration

You may use Composer to install Distillery into your Laravel project:

After installing Distillery, publish its config using the vendor:publish Artisan command.

After publishing Distillery's config, its configuration file will be located at config/distillery.php. This configuration file allows you to configure your application setup options and each configuration option includes a description of its purpose, so be sure to thoroughly explore this file.

Quickstart

Let's say you have a long list of products on route: /product-list all you need to do is attach Distillable trait to your Product model:

Distillable trait adds a static function distill($filters = null). In your controller that handles the /product-list route just replace your Product model fetch call (ie:Product:all()) with ::distill():

Pagination

distill() will return a paginated response of 15 items. This is the default Eloquent model value on $perPage property. You can adjust it by overwriting the value in your model or set a default value for limit in distillery model property.

To add pagination links to the view call $products->links(); in your blade template:

There you have it, a paginated list of Product models.

What!? This is just like Laravel's Paginator! Right, we'll want to filter it too, eh? Ok, carry on.

Filtering

If we want to filter the above product list with a search query on name and description we'll need a search filter for product model. Let's create it:

This will scaffold a Search filter in app/Filters/Product/Search.php

Generated class implements apply(Builder $builder, $value) method, that receives Eloquent builder and the filter value.

For the above Search example we would do something like this:

To apply the filter to the previous product list you can just add a search query string parameter to the url:

/product-list?search=socks and the collection will be automatically filtered and pagination links will reflect the set filters.

For more examples on filters check the Examples section.

How it works

The idea behind Distillery is that every request parameter is a filter name/value pair. Distillery loops through all request parameters, checks if the filter for selected model exists and builds a filtered resource collection based on their values.

By default Distillery predicts that you have:

All values are configurable through the config file.

Filter names

are reserved for laravel paginator.


Digging deeper

Artisan Command distillery:filter

Distillery comes with an Artisan generator command that scaffolds your filter classes for existing models. Signature has two parameters:

'distillery:filter {filter} {model?}'

If you pass in the model name the filter will be generated in the sub-namespace: App\Filters\{Model}. Without optional model paramater the filteres are generated in App\Filters for general usage on multiple models.

To enable fallback to general filters you need to set 'fallback' => true on the distillery model property.

During generation you'll be offered to choose from some standard filter templates:

Blank template

The generated class returns $builder without modifications. You'll need write the logic yourself.

Sorting template

You define a list of model fields you wish to sort on and select a default sorting field and direction.

Search template

Define the model field to search on. A filter with "like {$value}%" will be generated.

Serverside filter values

Sometimes you'll want to attach additional filters on server-side. By default you don't need to pass any filters in. Distilliery will pull them out of request. Usually you'll have a seo route, that already should return a fraction of models instead of all; like a category route for products: /category/summer-clothing?search=bikini.

Normally you wouldn't want to pass the category id in paramaters since it's already defined with a seo slug.

You can add aditional filters not defined in URI or overwrite those by passing an array into distill function. i.e.: If you have a Category filter that accepts an id, attach it in your controller:

Distillery Facade

Distillery comes with a facade that gives you an option to distill any model without the Distillable trait. It takes two parameters, Model FQN and filter array.

Model Resources

If you're using Distillery as API endpoints you probabbly don't want to expose your whole model to the world or maybe you want to attach some additional data. Distillery checks if Eloquent resources exist and maps the filtered collection to them, otherwise it returns normal models.

If you don't have them just create them with Artisan

and check out the docs on how to use them.

Default filter values per model

It is possible to define default filter values per model. For example if you want a default filter value for some model you can do it with a 'default' key in a protected $distillery array on the model itself:

Hide filters from URI QueryString

There is a 'hidden' config array available on model to hide filters from URI when those are applied serverside:

Enable fallback to general filters

A model can use a general filter if one in it's namespace isn't defined:

API Filter-Pagination Route

Distillery comes with a standard filtering route, where you can filter/paginate any model automatically without attaching traits to models.

This functionality is disabled by default. You need to enable it in the config.

Default route for filtering models is /distill use it in combination with model name and query string:

/distill/{model}?page=1&limit=10&search=socks

Models filterable by this route need to be added to the distillery.routing.models config array:

It's possible to change the route path in the config. If you want to protect it with Auth for example, you may also attach custom middleware to the route in config.

Customizing pagination links

Pagination links are part of the Laravel pagination. Check out the Laravel docs on how to customize them.


Examples

Sorting

For sorting a model on multiple fields you would have a sort filter with values something like this: sort=field-asc and sort=field-desc:

And apply to apply the filter just add it to the qs: /product-list?search=socks&sort=price-desc.

Filtering relations

Sometimes you'll want to filter on relations of the model.

Suppose you have a Product model with multiple colors attached:

And to apply it: /product-list?search=socks&sort=price-desc&color[]=2&color[]=5.

Roadmap to 1.0.0

License

Laravel Distillery is open-sourced software licensed under the MIT license.


All versions of laravel-distillery with dependencies

PHP Build Version
Package Version
Requires php Version ^7.1.3
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 matejsvajger/laravel-distillery contains the following files

Loading the files please wait ....