Laravel Cloud is here! Zero-config managed infrastructure for Laravel apps. Deploy now.

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.


Prettus L5 Repository Related Articles

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
Heartbeat Collection Method in Laravel 12.26; Wayfinder Now in React and Vue Starter Kits image

Heartbeat Collection Method in Laravel 12.26; Wayfinder Now in React and Vue Starter Kits

Read article
Using SQLite in production with Laravel image

Using SQLite in production with Laravel

Read article
All About the Filament v4 Beta Release image

All About the Filament v4 Beta Release

Read article
Kirschbaum logo

Kirschbaum

Providing innovation and stability to ensure your web application succeeds.

Kirschbaum
Acquaint Softtech logo

Acquaint Softtech

Acquaint Softtech offers AI-ready Laravel developers who onboard in 48 hours at $3000/Month with no lengthy sales process and a 100 percent money-back guarantee.

Acquaint Softtech
Laravel Cloud logo

Laravel Cloud

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

Laravel Cloud
PhpStorm logo

PhpStorm

The go-to PHP IDE with extensive out-of-the-box support for Laravel and its ecosystem.

PhpStorm
Shift logo

Shift

Running an old Laravel version? Instant, automated Laravel upgrades and code modernization to keep your applications fresh.

Shift
Get expert guidance in a few days with a Laravel code review logo

Get expert guidance in a few days with a Laravel code review

Expert code review! Get clear, practical feedback from two Laravel devs with 10+ years of experience helping teams build better apps.

Get expert guidance in a few days with a Laravel code review