devmakerlab / laravel-filters
Installs: 2 848
Dependents: 0
Suggesters: 0
Security: 0
Stars: 16
Watchers: 2
Forks: 0
Open Issues: 2
Requires
- php: ^7.4|^8.0
- illuminate/database: ^7.0|^8.0|^9.0|^10.0
Requires (Dev)
- devmakerlab/entities: ^3.1
- orchestra/testbench: ^6.19|^7.0|^8.2
- phpunit/phpunit: ^10.1
- symfony/var-dumper: ^5.3|^6.0|^7.0
This package is auto-updated.
Last update: 2024-10-26 13:11:22 UTC
README
DevMakerLab/Laravel-Filters
Need some filters? This package is based on the Repository Design Pattern to let you create specific queries easily.
Installation
⚠️ Requires >= PHP 7.4 ⚠️
composer require devmakerlab/laravel-filters
Usage
This package offers an abstract class AbstractFilterableRepository
which needs to be extended to implement the features of this package.
PeopleService.php
<?php ... $peopleRepository = new PeopleRepository($databaseManager); $people = $peopleRepository ->addFilter(OldPeopleFilter::class) ->get(['age' => 60]);
OldPeopleFilter.php
<?php declare(strict_types=1); use Illuminate\Database\Query\Builder; use DevMakerLab\LaravelFilters\AbstractFilter; class OldPeopleFilter extends AbstractFilter { public int $age; public function apply(Builder $queryBuilder): void { $queryBuilder->where('age', '>=', $this->age); } }
PeopleRepository.php
<?php declare(strict_types=1); use DevMakerLab\LaravelFilters\AbstractFilterableRepository; class PeopleRepository extends AbstractFilterableRepository { private DatabaseManager $databaseManager; public function __construct(DatabaseManager $databaseManager) { $this->databaseManager = $databaseManager; } public function get(array $args): array { $queryBuilder = $this->databaseManager->table('people') ->select(['firstname', 'lastname', 'age', 'gender']); $this->applyFilters($queryBuilder, $args); $people = $queryBuilder->get(); return $this->transform($people); } public function transform(Collection $people): array { $people->transform(function ($person) { return [ 'name' => sprintf('%s %s', $person->lastname, $person->firstname), 'age' => $person->age, 'gender' => $person->gender, ]; }); return $people->toArray(); } }
Example
Usage Example of DevMakerLab/Laravel-Filters package.