ymslavov / laravel-repository
Stateful repositories for Laravel models.
Installs: 1 408
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 2
Forks: 0
Open Issues: 0
Requires
- php: >=5.6.4
- hanneskod/classtools: ^1.1
- laravel/framework: 5.3.*
README
Based on the work featured in bosnadev.com, but with a few additional helpful methods for multi-record operations.
Laravel 5.3+ required.
Installation via Composer
composer require ymslavov/laravel-repository
Using Repository Generators
The package provides an easy way to generate repository classes for all models in the system.
All you need to do is register the service provider in your config/app.php in the 'providers' array:
'providers' => [
...other providers here...,
YasenSlavov\LaravelRepository\Providers\LaravelRepositoryServiceProvider::class
]
And then use the following command in your command line tool:
php artisan repositories:generate
The script will scan the app/ directory for any classes that extend the Eloquent Model class and create repository classes for them.
Extending the AbstractRepository directly
If you prefer not to use the auto-generated classes, you can simply extend the AbstractRepository class directly from the package.
Example:
class UsersRepository extends AbstractRepository {
/**
* Specify the fully-qualified model name. Best use Classname::class
*
* @return string
*/
function model()
{
return User::class;
}
}
$usersRepo = \App::make(UsersRepository::class);
$usersWithTitleManager = $usersRepo
->clearScope() //clear any state already established in the repo object
->pushCriteria(new ByRoleTitle('Manager'))
->all();