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


class ProductStrategy extends Strategy
{
    protected array $canOrderBy = ['name', 'price', 'created_at'];
    protected array $canWith = ['category', 'reviews'];
    protected array $allowedFields = ['id', 'name', 'price', 'category_id'];
    protected array $allowedAppends = ['discount_price'];

    protected array $config = [
        'name'     => ['filter' => ContainsClause::class],
        'price'    => ['column' => 'unit_price'],
        'category' => ['column' => 'category_id', 'explode' => true],
    ];
}

use function Myerscode\Laravel\QueryStrategies\filter;

public function index()
{
    return filter(Product::class)->with(ProductStrategy::class)->apply();
}