Download the PHP package larachimp/mango-repo without Composer
On this page you can find all versions of the php package larachimp/mango-repo. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package mango-repo
Mango Repo
Introduction
Mango Repo is an Eloquent Repository package that aims at bringing an easy to use and fluent API. Getting started with repository pattern can be quite overwhelming. This is especially true for newcomers to Eloquent who are getting the grasp of active record. Behind the scenes Mango Repo tries to use as much of the Eloquent API as possible and keeping things simple.
License
Mango Repo is open-sourced software licensed under the MIT license.
Version Compatibility
Laravel | MangoRepo |
---|---|
5.0.x | 0.2.x |
5.1.x | 0.2.x |
5.2.x | 0.2.x |
5.3.x | 0.2.x |
5.4.x | 0.2.x |
5.5.x | 0.2.x |
5.6.x | 0.3.x |
5.7.x | 0.3.x |
5.8.x | 0.4.x |
6.x | 1.x |
7.x | 2.x |
8.x | 3.x |
Installation
Install Mango Repo as you would with any other dependency managed by Composer:
Configuration
If you are using Laravel >= 5.5, you can skip service registration thanks to Laravel auto package discovery feature.
After installing Mango repo all you need is to register the and
in your config/app.php
configuration file:
Optionally you can register the annotations reader alias.
For more information check out the PineAnnotations package for more information on the annotation reader.
Creating a repository class
Use the command to create your repository classes. This command will take as argument the repository class namesapce (from App) and a option which allows you to specify the full namespace of the Eloquent model to which the repository will be tied.
The above command will generate the following repository class in the directory:
Notice the which specifies the Eloquent model the repository will make use of. If you would like to keep things a little bit simpler, the command allows you to specify an optional option which generates a repository class that uses annotations for specifying the Eloquent model:
The above command will generate the following repository class in the directory:
Using the repository
After creating your repository class, you may use it by resolving it via Laravel's Service container; either by dependency injection or by using the method.
In the following controller, we injected our repository in the constructor and used it from our index method:
Take note that the repository class can be injected in not only controllers' constructors, but also methods and any service which is resolved by the service container.
You can also use the , or method to resolve an instance of your repository class and use it as you please:
Although resolving repository classes from the service container seems the most efficient way to building up an instance, you may prefer to instantiate your repository classes manually for some reasons. To achieve this call the method on the new instance before using it.
The method will take care of loading the repository class dependencies for us:
Available Methods
Out of the box, repository classes comes with these methods already written for you. However, you are free to add your own methods or override existing methods in your repository class for building your own custom API and business logic.
To keep things as simple as possible, for many of theses methods, Mango Repo makes use of the same methods available on the Eloquent model. Hence, Mango Repo's API tries to be as close to Eloquent's API as possible.
Get all of the models from the database:
or:
Paginate the models from the database:
Paginate the models from the database into a simple paginator:
Save a new model and return the instance:
Update a model in the database. The update method accepts as its second argument either the model instance or the model id:
or:
Delete a record from the database.The delete method accepts as its first argument either the model instance or the model id:
or:
Find a Model in the Database using the ID:
or:
Find a model in the database or throw an exception:
or:
Find a model or models using some criteria:
or:
Gets the Eloquent model instance:
Model Repository Scoping
Mango Repo do not make use of long and tedious "Criterias classes" for filtering queries, instead any repository class created using the command can be "Model Scoped". In simpler terms this only means that you may access Local Query Scopes defined on your models directly on the repository class.
Hence, you define your query scopes once on your model classes and use them directly on your repository classes for query filtering.
Consider the following example:
Since we've defined a local scope on our User Model, we don't have to rewrite the same scope twice within our repository class. We simple use it directly on the repository class. Yes as simple as that!
You may even chain scopes and apply other filters as you would for any Eloquent model instance:
Going Further
We think we've done a good job here at creating a simple but yet rich boilerplate for creating repository classes and in most cases you would probably just create repository classes using the command like a breeze. However, if you still are not satisfied and require creating your custom repository classes that do not need to be Model Scoped and so on; fear not we've got you covered.
First start by creating a class that implements . Now you may implement all the methods available as you wish.
Remember you do not need to implement these methods again, you may use the trait which already implements those method for you:
If you would like the repository to be bootable use the trait, and for Model Scoping use :
Credits
Big Thanks to all developers who worked hard to create something amazing!
Creator
Twitter: @PercyMamedy
GitHub: percymamedy
All versions of mango-repo with dependencies
illuminate/support Version ^8.0
larachimp/pine-annotations Version ^3.0