A larvavel command for generating eloquent-repositories and their corresponding interface. I find the reposiory-pattern to be quite nice to abstract the data-layer from the application itself, so this artisan-command comes in handy.
The package can be installed via composer.
composer require mola/larepository
The service provider should be automatically added. If not please add
Mola\Larepository\LarepositoryServiceProvider::class,
to the providers-array in your config.php
.
If you want to change the default-paths publish the configuration-file.
php artisan vendor:publish --provider "Mola\Larepository\LarepositoryServiceProvider"
Laravel | Larepository |
---|---|
5.5.* | v1.*, v2.* |
The make:repository
-command will create a new repository with an associated interface.
The command will create a repository with a corresponding interface as well. This repository contains only a simple method to find all records for the specific table.
There are two commands provided within this package.
A command to create a repository-interface and an implementing class.
The command requires a name
-parameter which will be the name of the repository (the suffix 'Repository' will be appended automatically).
A Repositories
-directory will be created in the app-directory per default. The repositories will be placed there.
The default path can be overridden via the published configuration file.
The associated interface will be placed in the Contracts
-directory in the app-directory per default. The default path can be overridden in the configuration file as well.
The name
-parameter might be a Namespace as well, i.e.
php artisan make:repository Foo\\Bar
will create a Foo
-directory within the default/configured repository-directory. Within this directory a BarRepository.php
will be created.
The interface mirror that behaviour simply within the default/configured contracts-directory.
The command requires a model
-parameter as the second input-parameter. Per default the command expects the models to be in the app-directory.
The path can be modified in the configuration-file.
The model
-parameter can be a namespace as well. The command will look for it in the default/configured models-path.
To provide the User-model the command would be:
php artisan make:repository Foo\\Bar User
If your models reside in another directory-structure simply add the namespace to the parameter like so:
php artisan make:repository Foo\\Bar Bla\\Blub\\User
If you have a models-directory where all models reside in you should state the path in the published configuration-file.
If your models-directory is Models
for example and the directory was added in the configuration-file, all following commands will search for provided Models within this directory.
The package comes with a make:interface
-command. This command automatically creates an interface.
The command requires a name
-parameter which will be the name of the created interface (the suffix Interface
will be appended automatically).
The created interface will be placed in the Contracts
-directory in the app-directory per default. The default path can be overriden in the configuration file as well.
Interfaces can be created like this:
php artisan make:interface Foo\\Bar
The provided name may be a namespace as well. The above command would create the directory Foo
and place a BarInterface.php
within that directory.
The following methods are contained within every repository:
The findAll
-method retrieves all records from the corresponding table. It returns the records within a Laravel collection (API-documentation here).
$allEntries = $this->repository->findAll();