Download the PHP package dinkara/repobuilder without Composer
On this page you can find all versions of the php package dinkara/repobuilder. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download dinkara/repobuilder
More information about dinkara/repobuilder
Files in dinkara/repobuilder
Package repobuilder
Short Description Create Migration,Model and Repository from console
License MIT
Homepage https://github.com/dinkara/repobuilder
Informations about the package repobuilder
RepositoryBuilder
Repobuilder is addon for PHP Laravel framework that implements Repository pattern and adds an extra level for database communication in our project architecture. Instead of accessing models and writing queries in your Controller classes or Models, you should place them here. This way you will create single access point with controlled set of functions to your database. Library also has necessary Laravel commands for easy creation of complete set of classes and files (migrations, models and repositories).
Main advantages are:
- Code reusability
- Code transparency
- Global usage
- Scalable
Requirements
- Laravel ~5.5 or higher
Composer Install
Publish service
Add the service provider in config/app.php
:
For publishing new services you need to execute the following line
Now you are ready to use new features. To check if everything is fine, execute following command
If you can see make:repo option everything is ready.
How to use command
Examples :
In order to use your repositories as standard Laravel services you need to add following lines to your app/Providers/AppServiceProvider.php. This will point interface to proper class so Laravel knows which class to load when that interface is initialized.
This command will create new folder User under App\Repositories and make two new files inside of them. Interface IUserRepo and class EloquentUser.
This command will create same like first command but it will also create new migration with given name.
This command will create same like first command but it will also create new model with given name in App\Models.
This command will create migration, model and necessary repository classes.
How to use repositories
Repositories created with this library can be used in Controllers, Services, Seeders or any other class in your Laravel application.
Basic example:
Base interface with all available functions:
Function "__call" is a PHP magic function. It can be used to query database by attribute name.
Example:
This can only be used for "findBy" function and it is recommended to use it only in combination with attributes that are unique, because it returns only first record.
Adding custom function to your repositories
If you want to add custom or override existing functions in your repositories, this can be done easily by changing interface and class for specific repository.
Let's have a look at our User example:
You should first add new functions to App\Repositories\User\IUserRepo.php interface like shown below.
Afterwards you should define that functions in App\Repositories\User\EloquentUser.php class.
Repository pattern in this library is meant to cache state of your model object. This allows us to call multiple functions on the same model object (like we did with create and attachRole in the code above). To achieve this you have to return finalize function and pass it your model.
Do note that not all functions are meant to save state (for example when you have complex queries that return collection of data), in that case you should clear state of your model and just return eloquent result.