tohidhabiby/filters

This package helps you to create custom search on index method of controllers easily.

1.0.0 2021-12-05 16:06 UTC

This package is auto-updated.

Last update: 2025-02-05 23:45:37 UTC


README

This package helps you to create custom search on index method of controllers easily.

Installation

Use the package manager composer to install this package.

composer require tohidhabiby/filters

Usage

Each model should extend from BaseModel. Then create a filter class extended from Filters, the class should contain these parameters like this :

    /**
     * Registered filters to operate upon.
     *
     * @var array
     */
    protected array $filters = [
        'ids',
        ...
    ];
    
    /**
     * Define type of variables.
     *
     * @var array
     */
    public array $attributes = [
        'ids' => 'array',
        ...
    ];
    
    /**
     * @param string $email Email.
     *
     * @return Builder
     */
    protected function ids(array $ids) {
        return $this->builder->whereIn('id', $ids);
    }

Then in index method of controller you can do like this :

    /**
     * Display a listing of the resource.
     *
     * @param UserFilter $filters UserFilter.
     * @param Request    $request Request.
     *
     * @return AnonymousResourceCollection
     */
    public function index(UserFilter $filters, Request $request): AnonymousResourceCollection
    {
        return UserResource::collection(User::filter($filters));
    }

License

MIT