PHP code example of myerscode / laravel-query-strategies

1. Go to this page and download the library: Download myerscode/laravel-query-strategies 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/ */

    

myerscode / laravel-query-strategies example snippets


filter(Item::class)->with(MyStrategy::class);

Query::filter(Item::class)->with(MyStrategy::class);

new Filter(Item::query(), new MyStrategy, $request->query->all());

class Foo extends Model
{
    use IsFilterableTrait;

    public $strategy = BarStrategy::class;
}


$filter = (new Foo)->filter();

$filter->apply(); // Applies filter, order, limit, with methods and returns the paginated query
$filter->filter(); // Only applies filters and returns the Filter class
$filter->order(); // Only applies ordering and returns the Filter class
$filter->limit(); // Only applies limiting and returns the Filter class
$filter->with(); // Only applies 

// basic implamentation, that will just enable all default clauses for the strategy and will not mask the column name
$config = [
  'foo',
  'bar'  
];

// advance with custom methods, disabling clauses and changing the default clause
$config = [
    'name' => [
        'column' => 'first_name',
        'methods' => [
            'hello' => HelloClause::class,
            'world' => WorldClause::class,
        ]
    ],
    'surname' => [
        'column' => 'last_name',
        'disabled' => [
            'equals' => EqualsClause::class,
        ],
    ],
    'dob' => [
        'aliases' => [
            'date_of_birth',
            'birthday',
        ]
        'default' => FooBarClause::class,
    ],
    'address' => [
        'methods' => [
            'distance' => DistanceClause::class,
        ],
    ],
];

protected $defaultMethods = [
    ...
    LessThanClause::class => ['lessThan', '<', 'lt'],
    GreaterThanOrEqualsClause::class => ['greaterThanOrEquals', '>=', 'gte'],
    ...
];

// default value
protected $limitTo = 50;

// default value
protected $maxLimit = 150;

// default value
protected $canOrderBy = [
    'id',
];

?name[]=Fred&name[]=Tor&name[]=Chris&name--operator=not

// a strategy config with operator override properties
$config = [
    'name' => [
        'override' => 'name_override',
    ],
    'date' => [
        'overrideSuffix' => '--filter',
    ],
];
// name=Fred&name_override=like
// date=31/12/1987&date--filter=before

// a strategy config with operator override properties
$config = [
    'name' => [
        'explode' => true,
    ],
    'date' => [
        'explode' => true,
        'delimiter' => '||',
    ],
];
// name=Fred,Tor
// date=31/12/1987||12/07/1989

> php artisan vendor:publish --provider="Myerscode\Laravel\QueryStrategies\ServiceProvider" --tag=config

> php artisan make:strategy $name

> php artisan make:clause $name