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

dev-main 2023-05-23 10:20 UTC

This package is auto-updated.

Last update: 2025-06-23 15:19:33 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 mohammadahmadifar/filters:dev-main

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