1. Go to this page and download the library: Download dowob/laravel-refiner library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
dowob / laravel-refiner example snippets
use \Dowob\Refiner\Refinable;
use \Illuminate\Database\Eloquent\Model;
class Post extends Model {
use Refinable;
...
}
Post::refine()->get();
use \Dowob\Refiner\Definitions\Definition;
use \Dowob\Refiner\Enums\Like;
use \Illuminate\Support\Facades\DB;
...
public function definitions(): array
{
return [
// This allows exact (column = value) matching for `name` and enables sorting.
// Note that any search that is a string will be trimmed by default.
Definition::make('name')->search()->sort(),
// You can disable trimming per-definition with trim(false)
Definition::make('non-trimmed-name')->search()->trim(false),
// This allows LIKE matching without sorting. By default, LIKE sorting will use Like::BOTH however,
// you can override this by passing a Like::* value as the first parameter as shown in 2nd example.
Definition::make('email')->searchLike(), // LIKE %value%
Definition::make('email-match-after')->searchLike(Like::END), // LIKE value%
// You can specify a different column name to use rather than the name used in the request parameter.
// This also demonstrates support for WHERE column IN (...) type searches
Definition::make('type')->column('account_type')->searchIn(),
// If a pre-defined search option just doesn't cut it for you, you can specify a custom callback
// that will be used for the search.
Definition::make('full-name')->searchCustom(function (Builder $query, mixed $value) {
$query->where(DB::raw('CONCAT(first_name, last_name)'), 'like', '%' . $value . '%');
}),
// It can also access any scopes etc. as you normally would be able to on the model
Definition::make('model-scope')->searchCustom(function (Builder $query, mixed $value) {
$query->aScopeOnTheModel($value);
}),
// You may want a search to always be applied, even if the search value isn't present in the query.
// You can do this by specifying `alwaysRun` like so. Without the `alwaysRun`, this query would
// not apply the `where('active', 1)` if there's no search for `active` present in the request!
Definition::make('active')->alwaysRun()->searchCustom(function (Builder $query, mixed $value) {
switch ($value) {
case 'inactive':
$query->where('active', 0);
break;
case 'all':
// No action, show both active & inactive
break;
default:
// Reached either by no value being specified, or it being 'active'
// or anything that does not match the above
$query->where('active', 1);
}
}),
];
}
use \Dowob\Refiner\Definitions\Definition;
// Three ways of specifying the validation rules that result in same validation
Definition::make('email')->search()->validation(['
use \Dowob\Refiner\Definitions\Definition;
use \Illuminate\Contracts\Database\Query\Builder;
// A definition using a custom search that can handle multiple query values due to its validation rules.
Definition::make('date')
->validation([
'start' => [
'ed validation
if (empty($value['start'])) {
return $query->where('created_at', '=<', $value['end']);
}
if (empty($value['end'])) {
return $query->where('created_at', '>=', $value['start']);
}
// Value contains both 'start' and 'end'
$query->whereBetween('created_at', $value);
});
use \Dowob\Refiner\Enums\Sort;
...
public function defaultSorts(): array
{
return [
// The first value in the array must match the relevant sort-enabled definition
// The second value is the sort direction to use as default.
['last_name', Sort::ASC],
['first_name', SORT::ASC],
];
}
use \Dowob\Refiner\Facades\Registry;
// Take the refiner from the end of the registry (last in)
Registry::pop();
// Take the refiner from front of the registry (first in)
Registry::shift();
// Example
User::refine();
Post::refine();
Registry::shift(); // returns the UserRefiner
Registry::shift(); // returns the PostRefiner, as we've already shifted the UserRefiner out of registry.
// Example of registry setup in this order,
// each 'we can retrieve it by' assumes no prior models are retrieved in the example.
// 1st registered, 1st UserRefiner/User registered
User::refine();
// We can retrieve it by...
// - Registry::shift()
// - or Registry::shift(UserRefiner::class);
// - or Registry::shift(model: User::class);
//
// 2nd registered, 1st PostRefiner/Post registered
Post::refine();
// We can retrieve it by...
// - Registry::shift(PostRefiner::class)
// - or Registry::shift(model: Post::class);
//
// 3rd registered, 2nd UserRefiner/User registered
User::fine();
// We can retrieve it by...
// - Registry::pop(UserRefiner::class)
// - or Registry::pop(model: User::class);
//
// 4th registered, 2nd PostRefiner/Post registered
Post::fine();
// We can retrieve it by...
// - Registry::pop()
// - or Registry::pop(PostRefiner::class)
// - or Registry::pop(model: Post::class);