L5 Repository
L5 Repository stats
- Downloads
- 6.8M
- Stars
- 4,106
- Open Issues
- 246
- Forks
- 866
Laravel 5|6|7|8|9|10 - Repositories to the database layer
Laravel 5 Repositories
The Laravel 5 Repositories package provides an elegant way to abstract the data layer of your application, enhancing maintainability and flexibility by utilizing the Repository pattern. This package simplifies data manipulation across various models and promotes cleaner and more scalable code.
Key Features
- Repository Pattern Implementation: Centralize data access and business logic, making code more maintainable.
- Criteria System: Allows encapsulation of specific conditions into criteria objects, which can be dynamically applied to repositories.
- Presenter and Transformer Layers: Offers a way to format or transform data before it reaches the end user.
- Query Caching: Improves performance by caching query results, automatically handling common actions like clearing cache on updates.
- Automatic Validation: Leverages a validation system that integrates seamlessly into your repositories, ensuring data integrity.
Installation
Via Composer
Run the following command to install the package:
composer require prettus/l5-repository
Laravel Configuration
For Laravel 5.5 and above, the service provider will automatically get registered. For older versions, add the service provider to your config/app.php:
'providers' => [ ... Prettus\Repository\Providers\RepositoryServiceProvider::class,],
To publish the configuration file, use:
php artisan vendor:publish --provider="Prettus\Repository\Providers\RepositoryServiceProvider"
Usage
Creating Models and Repositories
Create your Eloquent models as usual and then create corresponding repositories:
namespace App\Repositories; use Prettus\Repository\Eloquent\BaseRepository; class PostRepository extends BaseRepository { public function model() { return "App\\Post"; }}
Implementing Criteria
Criteria can be used to apply specific conditions to your queries:
use Prettus\Repository\Contracts\CriteriaInterface;use Prettus\Repository\Contracts\RepositoryInterface; class MyCriteria implements CriteriaInterface { public function apply($model, RepositoryInterface $repository) { return $model->where('user_id', auth()->id()); }}
Apply criteria in controllers:
$posts = $this->repository->pushCriteria(new MyCriteria())->all();
Query Caching
Enable caching in your repository to improve performance:
use Prettus\Repository\Traits\CacheableRepository;use Prettus\Repository\Contracts\CacheableInterface; class PostRepository extends BaseRepository implements CacheableInterface { use CacheableRepository;}
Using Presenters and Transformers
Define a transformer:
use League\Fractal\TransformerAbstract; class PostTransformer extends TransformerAbstract { public function transform(Post $post) { return [ 'id' => (int) $post->id, 'title' => $post->title, 'content' => $post->content, ]; }}
Attach a presenter in the repository:
use Prettus\Repository\Presenter\FractalPresenter; class PostPresenter extends FractalPresenter { public function getTransformer() { return new PostTransformer(); }}
Additional Tools
- Artisan Commands: Simplify the creation of entities related to the repository pattern with commands like
php artisan make:entity Post, which sets up models, controllers, and more. - Automatic Validation: Easily specify validation rules directly in the repository or use a separate validation class.
- Request Criteria: Filter and sort API requests dynamically by extending the
RequestCriteriaclass.
This package is a powerful tool for managing the data access layer of Laravel applications, promoting best practices like DRY (Don't Repeat Yourself) and separation of concerns.