Download the PHP package touhidurabir/laravel-model-repository without Composer
On this page you can find all versions of the php package touhidurabir/laravel-model-repository. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download touhidurabir/laravel-model-repository
More information about touhidurabir/laravel-model-repository
Files in touhidurabir/laravel-model-repository
Package laravel-model-repository
Short Description A package to implement repository pattern for laravel models
License MIT
Informations about the package laravel-model-repository
Laravel Model Repository
A simple package to use Repository Pattern approach for laravel models .
Repository pattern
Repositories are classes or components that encapsulate the logic required to access data sources. They centralize common data access functionality, providing better maintainability and decoupling the infrastructure or technology used to access databases from the domain model layer. Microsoft
Installation
Require the package using composer:
To publish the config file:
Command and Configuration
To use this package, you need to have repository class bound to laravel model class . This package includes a command that make it easy to to create repository classes from command line . to create a new repository class, run the following command
The above command will create a new repository UserRepository class in App\Repositories path . the --model option to define which laravel model class to target for this repositoty class . The content of UserRepository will look like
This package by default assume all models are located in path App\Models and use the path App\Repositories to store the repository classes. But also possible to provide custom repositories class path and different model class path . for example
The above command will try to store the repository class to path App\SomeOtherPath and will create a directory named SomeOtherPath if not already exists. Will also try to resolve model path/namespace from App\OtherModelPath .
Check the config file after publishing at the config/model-repository.php to see the default settings configurations .
It is also possible to replace an existing repository file via the command when passing the flag --replace
The above command will replace the already exiting UserRepository.php at the given path with newly generated one.
Be ery carefull of the replacing ability as if your repository class contains any custom code, that will be fully replaced with a newly generated file and those custom changes will be lost.
Usage
The best way to use the repository classes via Dependency Injection through the controller classes . for example :
And in that way one can already get a fully qualified user repository class . Also to manually initiated :
Or through static constructor
The repository class will have following features/abilities .
Create
To create a new model record, just call the create method on repositoty class and pass the data attributes as :
Update
To update a existing model record, call the update method of the repository class . the update method will require 2 params , the data attributes and the model redored primary key value or an exiting model instance .
To update with primary key for user with primary key of id with value 10
or To update the already retrived model record :
Find
To find a model record, use the find method of the repository class
The find method can also work with array where it will use those as AND WHERE query and return the first record that match
By passing the optional relations array as the second argument to find method will load the relations along with model record
The thrid agument is a optional boolen which is by default set to false . By setting it to true, it will thorw the \Illuminate\Database\Eloquent\ModelNotFoundException when a model record not found .
All Records
To get back all records, use the all method of repository class
Delete
To Delete a model record, use the delete method of repository class
The delete method can wrok with model instance or the same kind of argument passed to the repository class find method .
The delete method also check for the SoftDelete feature , that is if the model is using the Illuminate\Database\Eloquent\SoftDeletes trait, the it will do the soft delete of given model records.
Force Delete
To Force Delete a model record, use the forceDelete method of repository class
The delete method can wrok with model instance or the same kind of argument passed to the repository class find method .
The delete method also check for the SoftDelete feature, that is regardless of the model is using the Illuminate\Database\Eloquent\SoftDeletes trait, the it will remove those records from DB.
Restore
To Restore a model record that has soft deleted, use the forceDelete method of repository class
The restore will only works for those models that use the SoftDeletes feature . It try to use the restore on the model that do not have SoftDeletes implemented, it will throw an exception.
The restore method can wrok with model instance or array of model primary keys .
Other Features
Get Model
As this package does not handle all of the features of Eloquent and if any other Eloquent method need to use to build complex query, we need the model instance . to get the model instance
Also to set/update the model later
Model Sanitizer
The BaseRepository class includes a model sanitizer that will automatically sanitize passed array attributes on model record create/update . Here sanatize means it will remove any element from the data array to match with the model table schema while at the same time respecting model $fillable and $hidden properties .
The implementation of these methods are as such
So even if extra details passed, it will be ignored or some columns passed that in the $fillable or $hidden list.
The above code will run without any issue while a simple model create method will throw exception .
This become very useful when in one single controller method do need to push data to multiple model table
Contributing
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
Please make sure to update tests as appropriate.
License
MIT