A repository pattern implementation for Laravel Framework
Via Composer
$ composer require bigpaulie/repository
Publish the configuration file
$ php artisan vendor:publish --provider=bigpaulie\\repository\\RepositoryServiceProvider --tag=repository.config
package version | laravel version |
---|---|
2.x | 6.9 or newer |
1.x | 5.x |
A repository class is any class that extends bigpaulie\repository\AbstractRepository
The general rule of thumb is that your repository should have the same name as your model by with a suffix of "Repository".
Let's say we have the following case, we have a model named Person, than the repository class should be named PersonRepository
class PersonRepository extends AbstractRepository {}
You can generate a repository for your model using the provided artisan command
php artisan repository:generate Person
The above command will generate a repository class called PersonRepository.
Find a specific resource by it's ID
/** @var PersonRepository $repository */
$repository = new PersonRepository();
/** @var Person|null $person */
$person = $repository->find(1);
Get all results for this resource
/** @var PersonRepository */
$repository = new PersonRepository();
/** @var Illuminate\Database\Eloquent\Collection|Person[] */
$persons = $repository->all();
Create's a new resource and return the database object, if your model doesn't allow mass assigning of attributes, use false
as the second parameter.
/** @var PersonRepository $repository */
$repository = new PersonRepository();
/** @var Person $person */
$person = $repository->create([
'name' => 'Popescu Ion',
'age' => 30
]);
Update a specific resource by it's ID, you can also pass a model instance as a second parameter.
/** @var PersonRepository $repository */
$repository = new PersonRepository();
/** @var Person $person */
$person = $repository->update([
'name' => 'Popescu Marin',
'age' => 33
], 1);
Delete a specific resource by it's ID, you can also force delete by passing true
as the second parameter.
/** @var PersonRepository $repository */
$repository = new PersonRepository();
try {
/** @var Person $person */
$person = $repository->delete(1);
} catch (RepositoryException $exception) {
// do something if operation fails
}
You can use the helper function by providing the FQDN of a repository or a model.
If a repository exists for a given model, an instance of the repository is returned otherwise an abstract repository is returned allowing you to preform all the builtin CRUD functionality.
The Person model has a PersonRepository
/** @var PersonRepository $person */
$personRepository = repository(PersonRepository::class);
/** @var PersonRepository $person */
$personRepository = repository(Person::class);
The Dog model doesn't have a repository
/** @var bigpaulie\repository\Repository $dog */
$repository = repository(Dog::class);
Check out our wiki
Please see the changelog for more information on what has changed recently.
$ ./vendor/bin/phpunit -c phpunit.xml
Please see contributing.md for details and a todolist.
If you discover any security related issues, please email author email instead of using the issue tracker.
license. Please see the license file for more information.