Skip to content

Latest commit

 

History

History
86 lines (66 loc) · 1.77 KB

filters.md

File metadata and controls

86 lines (66 loc) · 1.77 KB
title github order
Filters
8

Filters are responsible for transforming the current request to a database query.

Creating Filters

You can generate new filter classes by calling the root:filter artisan command:

php artisan root:filter Category

Registering Filters

You can register filters in resources and extracts, by using the filters method.

use App\Root\Filters\Category;
use Illuminate\Http\Request;

/**
 * Define the filters for the resource.
 */
public function filters(Request $request): array
{
    return [
        Category::make(),
    ];
}

Configuration

Options

Filters have an option method, where you can define the selectable options:

namespace App\Root\Filters;

use Cone\Root\Filters\Filter;
use Illuminate\Http\Request;
use Illuminate\Database\Eloquent\Builder;

class Category extends Filter
{
    /**
     * Apply the filter on the query.
     */
    public function apply(Request $request, Builder $query, mixed $value): Builder
    {
        return $query->where('category', $value);
    }

    /**
     * Get the filter options.
     */
    public function options(Request $request): array
    {
        return [
            'product' => 'Product',
            'service' => 'Service',
        ];
    }
}

Also, you may configure a select filter with multiple selectable choices. To do so, call the multiple method on the filter instance:

Category::make()->multiple();

Authorization

You may allow or disallow interaction with filters. To do so, you can call the authorize method on the filter instance:

$filter->authorize(static function (Request $request): bool {
    return $request->user()->isAdmin();
});