Package Data | |
---|---|
Maintainer Username: | saritasa |
Maintainer Contact: | sergey@saritasa.com (Sergey Populov) |
Package Create Date: | 2017-04-07 |
Package Last Update: | 2021-03-09 |
Language: | PHP |
License: | MIT |
Last Refreshed: | 2024-05-09 15:00:10 |
Package Statistics | |
---|---|
Total Downloads: | 12,201 |
Monthly Downloads: | 77 |
Daily Downloads: | 2 |
Total Stars: | 3 |
Total Watchers: | 7 |
Total Forks: | 2 |
Total Open Issues: | 1 |
Implementation of Repository pattern for Laravel (on top of Eloquent)
Install the saritasa/laravel-repositories
package:
$ composer require saritasa/laravel-repositories
php artisan vendor:publish --tag=laravel_repositories
return [
'bindings' => [
\App\Models\User::class => \App\Repositories\UserRepository::class,
],
];
Note: Your custom repository must implement IRepository
contract.
To get specific repository in your code you can just build with DI container repositories factory and then
build needed for your repository in this factory.
Example:
$repositoryFactory = app(\Saritasa\LaravelRepositories\Repositories\IRepositoryFactory::class);
$userRepository = $repositoryFactory->getRepository(\App\Models\User::class);
Methods findWhere/getWhere/getWith/getPage/getCursorPage/count/ can receive criteria as params Here the examples of available syntax:
$criteria = [
'field1' => 'value1',
'field2' => 1,
];
In this case =
operator and and
boolean between them will be used.
Example: ... 'field1 = 'value1' and 'field2' = 1 ...
$criteria = [
['field1', '<>', 'value1'],
['field2', '>', 1, 'or'],
['field3', 'in', [1, 2]],
['field4', 'not in', new \Illuminate\Support\Collection([1, 2])],
];
Important:
arrays and collection can be used only with in
and not in
operators.
Note: As 4th parameter you can pass boolean or
/and
(and
uses by default).
But you should remember that boolean used between current and previous criterion
Example: ... 'field1 <> 'value1' or 'field2' > 1 and 'field3' in (1, 2) and 'field4' not in (1, 2) ...
$criteria = [
new Criterion([
Criterion::OPERATOR => '<>',
Criterion::VALUE => 'value1',
Criterion::ATTRIBUTE => 'field1',
]),
new Criterion([
Criterion::OPERATOR => '>',
Criterion::VALUE => 1,
Criterion::ATTRIBUTE => 'field2',
Criterion::BOOLEAN => 'or',
]),
new Criterion([
Criterion::OPERATOR => 'in',
Criterion::VALUE => [1, 2],
Criterion::ATTRIBUTE => 'field3',
]),
new Criterion([
Criterion::OPERATOR => 'not in',
Criterion::VALUE => [1, 2],
Criterion::ATTRIBUTE => 'field4',
]),
];
Result will be the same as in previous example.
$criteria = [
[
['field1', '<>', 'value1'],
['field2', '>', 1, 'or'],
],
[
['field3', 'in', [1, 2]],
['field4', 'not in', [1, 2],
'boolean' => 'or',
],
];
Note: you can add nesting level in any depth what you want. To use or
condition between one group
and other(group and non-group condition) you can pass 'boolean' parameter in the same level as other conditions.
Example:... ('field1 <> 'value1' or 'field2' > 1) or ('field3' in (1, 2) and 'field4' not in (1, 2)) ...
Method getWith() method allows to retrieve list of entities with
eager loaded related models and related models counts. Also allows to filter this list by given criteria
and sort in requested order.
Example:
$usersRepository->getWith(
['role', 'supervisors'],
['phones'],
[],
new Saritasa\LaravelRepositories\DTO\SortOptions('name', 'DESC')
);
Base exception for repository layer.
Throws when can not register custom repository.
Throws in case when some model not exists in storage.
Throws when provided criteria has incorrect format at least in one criterion inside.
If you have GNU Make installed, you can use following shortcuts:
make cs
(instead of php vendor/bin/phpcs
) -make csfix
(instead of php vendor/bin/phpcbf
) -make test
(instead of php vendor/bin/phpunit
) -make install
- instead of composer install
make all
or just make
without parameters -