asp-devteam / laravel-repository
Library created to provide an easy way to develop a Model's CRUD in laravel.
Requires
- php: ^7.2.5
- flugger/laravel-responder: ^3.1
- illuminate/support: ~7
Requires (Dev)
- fzaninotto/faker: ^1.4
- mockery/mockery: ^1.3.1
- nunomaduro/collision: ^4.1
- nunomaduro/phpinsights: ^1.14
- phpunit/phpunit: ^9.0
- v7.x-dev
- v7.2.13
- v7.2.12
- v7.2.11
- v7.2.10
- v7.2.9
- v7.2.8
- v7.2.7
- v7.2.6
- v7.2.5
- v7.2.4
- v7.2.3
- v7.2.2
- v7.2.1
- v7.2.0
- v7.1.0
- v7.0.2
- v7.0.1
- v7.0.0
- v6.x-dev
- v6.4.0
- v6.3.13
- v6.3.12
- v6.3.11
- v6.3.10
- v6.3.9
- v6.3.8
- v6.3.7
- v6.3.6
- v6.3.5
- v6.3.4
- v6.3.3
- v6.3.2
- v6.3.1
- v6.3.0
- v6.2.2
- v6.2.1
- v6.2.0
- v6.1.12
- v6.1.11
- v6.1.10
- v6.1.9
- v6.1.8
- v6.1.7
- v6.1.6
- v6.1.5
- v6.1.4
- v6.1.3
- v6.1.2
- v6.1.1
- v6.1.0
- v6.0.1
- v6.0.0
- v0.x-dev
- v0.1.31
- v0.1.30
- v0.1.29
- v0.1.28
- v0.1.27
- v0.1.26
- v0.1.25
- v0.1.24
- v0.1.23
- v0.1.22
- v0.1.21
- v0.1.20
- v0.1.19
- v0.1.18
- v0.1.17
- v0.1.16
- v0.1.15
- v0.1.14
- v0.1.13
- v0.1.12
- v0.1.11
- v0.1.10
- v0.1.9
- v0.1.8
- v0.1.7
- v0.1.6
- v0.1.5
- v0.1.4
- v0.1.3
- v0.1.2
- v0.1.1
- v0.1.0
- dev-master
This package is auto-updated.
Last update: 2025-03-11 20:07:05 UTC
README
This is a library created to provide an easy way to develop a Model's CRUD in laravel.
It provides several traits and classes to allow you to create Controllers that handle automaticaly pagination and
create JSON responses in a standard format. Also, it provides a set of traits to implement what we call Model driven
Repositories, which allows to have your Models act as data Repositories that can handle filtering, pagination, CRUD
operations as well as provide an easy way to create your own custom operations and validations.
Requirements
This package requires:
- PHP 7.2+
- Laravel 7+ or Lumen 7+
Install
The compatibility with Laravel is as following
Laravel | laravel-repository |
---|---|
5.7 | ^0.1 |
5.8 | ^0.1 |
6.x | ^6.0 |
7.x | ^7.0 |
To add dependency to a Laravel 7.x project, run
composer require asp-devteam/laravel-repository "^7.0"
Laravel
Register Service Provider
Append the following line to the providers
key in config/app.php
to register the package:
ASP\Repository\RepositoryServiceProvider::class,
The package supports auto-discovery, so if you use Laravel 5.5 or later you may skip registering the service provider and facades as they will be registered automatically.
Publishing resources
To publish the available translations and config to your application, for customization, just run:
php artisan vendor:publish --tag=repository.translations
php artisan vendor:publish --tag=repository.config
Usage
Write a few lines about the usage of this package.
This documentation assumes some knowledge of how Fractal works.
Extending ASP\Repository\Base\Controller
The package has a Controller
class, which implements a middleware that handles pagination on index:
use ASP\Repository\Base\Controller;
class YourController extends Controller
{
}
Using Model Repository
Option 1: Using the provided Repository
To use the Repository you can use it in your model class:
use ASP\Repository\Traits\Repository;
class YourModel extends Model
{
use Repository;
}
This will make available to you several methods:
getAllRecords(Filter $filters = null, array $pagination = null)
getRecordById($id)
createRecord(Request $request)
updateRecordById($id, Request $request)
deleteRecordById($id, Request $request)
Option 2: Creating your own Repository
You can also extend the Repository and add your own methods, this also allows you to use Model Validators:
use ASP\Repository\Traits\Repository;
...
trait YourRepository
{
use Repository;
}
use ASP\Repository\Traits\Repository;
class YourModel extends Model
{
use YourRepository, YourModelValidator;
}
Using Validators
use ASP\Repository\Traits\Validator;
trait YourModelValidator
{
use Validator;
/**
* @return void
*/
protected static function boot()
{
parent::boot();
self::setBaseRules(
[
'name' => [
'laravel rules here'
],
]
);
}
/**
* @return array
*/
protected static function getCustomRules()
{
$rules = self::getBaseRules();
return array_merge(
$rules,
[
'new rules'
]
);
}
}
Using Filters
To filter your Model queries you can extend ASP\Repository\Base\Filter
:
use ASP\Repository\Base\Filter;
class PetFilters extends Filter
{
public function <filterName>(<$parameters>)
Contributing
Please see CONTRIBUTING for details.
Security
If you discover any security-related issues, please email asp-devteam@alter-solutions.com instead of using the issue tracker.
License
The MIT License (MIT). Please see License File for more information.
Copyright 2020 Alter Solutions Portugal