Download the PHP package pp-spaces/laravel-repository without Composer
On this page you can find all versions of the php package pp-spaces/laravel-repository. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download pp-spaces/laravel-repository
More information about pp-spaces/laravel-repository
Files in pp-spaces/laravel-repository
Package laravel-repository
Short Description Repository Design Pattern implementation for Laravel
License MIT
Homepage https://pp-spaces.github.io/laravel-repository/
Informations about the package laravel-repository
Laravel Repository Design Pattern
Contents
- What is the Repository Design Pattern?
- Usage
- Installation
- Make a repository
- Use Case
- Help
Upgrade Notice
NOTE: The reason why I modified how the
Repository
is being created because I want theRepository
to useRoute Model Binding
for faster data query.
- How to upgrade?
What is the Repository Design Pattern
To put it simply, it is an implementation of a brokering layer between the application and a data source. Neither party needs to be be aware of the other to perform their respective jobs which allows us to have a decoupled architecture which in turn helps in the scaling of the application in the big leagues without having hard dependencies.
Is it the magic bullet
Well, no it is not. Like every design pattern it has its ups and downs, pros and cons.
Pros:
- Separation of concerns; the application need not know about or track any or all data sources.
- Allows easy unit testing as the repositories are bound to interfaces which are injected into classes at run time.
- DRY (Dont Repeat Yourself) design, the code to query and fetch data from data source(s) is not repeated.
Cons:
- Adds another layer of abstraction which adds a certain level of complexity making it an overkill for small applications.
Source
Usage
This package provide a command-line interface for you to create repository in your Laravel application.
Installation
Require pp-spaces/laravel-repository
package to your laravel installation
Make a repository
Run the following command to generate repository:
To make model repository simply run:
Use Case
How to use Repository
Create your repository, e.g. UserRepository
for User
model:
Update UserRepository
logic:
NOTE: Check
PPSpaces\Repositories\Model
for available methods that you may override. Keep in mind that you still have access to all Model instance that you've created. The$this->user
is the instance of your\App\User
model.
Within your UserController
assume you have a resource controller created. Inject the UserRepository
to the contoller. Now you can access the repository in your controller method:
Or alternatively, you may use Route Model Binding on the controller actions whose type-hinted
variable names match a route segment name.
Read more about Route Model Binding here
How to upgrade?
Upgrade from
v0.0.9
or earilier tov1.0.0