Download the PHP package aerynl/refinement without Composer
On this page you can find all versions of the php package aerynl/refinement. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download aerynl/refinement
More information about aerynl/refinement
Files in aerynl/refinement
Package refinement
Short Description Easy refinements for use with Laravel
License MIT
Homepage http://github.com/aerynl/refinement
Informations about the package refinement
Refinement package
Short description
Easy refinements for use with Laravel. Contains functions for saving filters in Session, generating filter options and fetching filter results.
Quick start
Second command will create \app\config\packages\aerynl\refinement\config.php
file, where you can specify table connections and filter names.
How to use
If you want to save filters in session, run:
If you want to get filtered results, run:
If you want to get refinement options, run:
What to write in config file
In config file you can specify table connections in joins
array and filter names in titles
array.
Table connections
Sometimes we need to make joins to create the necessary queries.
Because it's hard to create proper queries from the inputted parameters the configuration below will be used to create the joins.
Be sure all the tables you are using for filtering have join configuration!
Note, this doesn't mean you should specify in join array tables, which are used to get options. For example, if you filter products by product.color_id
and you have table colors
, where all colors are kept, there is no need to add colors
table to joins array. But if you always need to show only that products, which have product_sale.active
= false
, be sure that product_sale
table is specified in config file.
Joins array has the following scheme:
Titles of filters
Configure the name of your refinements here. If no name is entered the standard naming format will be used (ucfirst)
Example:
Refinement title translations
Create a file called refinements.php in /app/lang/en
, there you'll be able to add translations for the titles you've added to the configuration.
Detailed information about functions
Refinement::updateRefinements($session_name, $new_refinements)
This method is used to save filters in session.
$session_name
is the name of session, so you can use several filters sessions in one application.
$new_refinements
is a new filters array, that should be remembered. Keep the following scheme to have package work correctly:
Refinement::getRefinedQuery($main_model, [$session_name, $eager, $additional_wheres, $additional_joins, $refinements_array])
This method is used to generate query for getting filtered results. Why doesn't it return filtered results? Sometimes you will need to do some operations with query before getting the results, for example groupBy, orderBy, paginate and others. So you can use this function in the following way:
Passed variables:
$main_model
- string main model name.$session_name
- string session name.$eager
- array of tables, which you want to eager load. http://laravel.com/docs/4.2/eloquent#eager-loading$additional_wheres
- array of additional where conditions, which need to be ran. For examplearray("products.active is true", "products.deleted is null")
. Note, if your conditions use other tables, except of main one, you need to specify these tables in$additional_joins
(and in config file).$additional_joins
- array of additional tables, which are used in$additional_wheres
. Note, these tables need to be configured in config file.$refinements_array
- is used in case you need to get query with filters not from session, but from custome array. Should have the same format as$new_refinements
inupdateRefinements
function. In this case session refinements are not used.
Refinement::generateOptionsArray($main_model, [$options_scheme, $session_name, $eager, $additional_wheres, $additional_joins])
This method is used for generating array of refinements options using passed scheme. It returns array of options with the following scheme:
$column_name
, $parent_table
and $option_id
are used for creating $new_refinements array for updateRefinements function.
For example you can show options like checkboxes with name="$parent_table[$column_name][]"
and value="$option_id"
.
Passed variables:
Note, first 5 variables should be the same as for getRefinedQuery
function if you want to have actual $number_of_results.
$main_model
- string main model name.$session_name
- string session name.$eager
- array of tables, which you want to eager load. http://laravel.com/docs/4.2/eloquent#eager-loading$additional_wheres
- array of additional where conditions, which need to be ran. For examplearray("products.active is true", "products.deleted is null")
. Note, if your conditions use other tables, except of main one, you need to specify these tables in$additional_joins
(and in config file).$additional_joins
- array of additional tables, which are used in$additional_wheres
. Note, these tables need to be configured in config file.$options_scheme
- scheme of options we want to select in the following format:
e.g.
This means, that results are filtered by products.color_id
. And color names you can see in colors.color_name
column.
What will be done soon
- Laravel 5 supporting
- While I was writing this documentation, I had a great idea! We can avoid passing so many parameters each time by changing static methods to non-static.