L5 Repository

prettus/l5-repository image

L5 Repository stats

Downloads
6.8M
Stars
4,106
Open Issues
246
Forks
866

View on GitHub →

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 RequestCriteria class.

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.

Cube

Laravel Newsletter

Join 40k+ other developers and never miss out on new tips, tutorials, and more.


Prettus L5 Repository Related Articles

An Opinionated Agent Skill for Building REST APIs in Laravel image

An Opinionated Agent Skill for Building REST APIs in Laravel

Read article
Pretty PHP Info: A Modern Replacement for `phpinfo()` image

Pretty PHP Info: A Modern Replacement for `phpinfo()`

Read article
Stop Failing APIs from Killing Your Queue — Fuse for Laravel image

Stop Failing APIs from Killing Your Queue — Fuse for Laravel

Read article
Backpack v7 is Launched - See What's New image

Backpack v7 is Launched - See What's New

Read article
Blocking invalid requests to your Laravel application image

Blocking invalid requests to your Laravel application

Read article
Use Amazon's new Fair Queue Feature With SQS in Laravel 12.27 image

Use Amazon's new Fair Queue Feature With SQS in Laravel 12.27

Read article
Laravel Cloud logo

Laravel Cloud

Easily create and manage your servers and deploy your Laravel applications in seconds.

Laravel Cloud
The Certification of Competence for Laravel logo

The Certification of Competence for Laravel

A community-driven, proctored assessment across 4 levels designed to validate real-world Laravel knowledge, from Junior to mastery-level Artisan. Official Vue.js, Official Nuxt, Angular, React, JS certifications also available.

The Certification of Competence for Laravel
Statamic logo

Statamic

The drop-in ready Laravel CMS you’re been waiting for. Go full-stack or headless, flat file or database – it’s up to you.

Statamic
Typesense Search logo

Typesense Search

Typesense is an open source, blazing-fast search engine, optimized for helping you build delightful search experiences for your sites and apps. Natively integrated with Laravel Scout.

Typesense Search
Celebian logo

Celebian

Celebian is a social media marketing agency specializing in helping their clients go viral on TikTok. Whether you're looking to reach a bigger audience or gain more Tiktok followers, likes, and views, they've got you covered.

Celebian
Blastup logo

Blastup

Blastup provides social media enhancement services including buying Instagram likes, followers, and views, with features like instant delivery and a variety of packages to suit different needs.

Blastup