Package

Prettus L5 Repository

Laravel 5|6|7|8|9|10 - Repositories to the database layer

Package

prettus/l5-repository

View on GitHub →

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

Packistry is a Self-hosted Composer Repository Made with Laravel image

Packistry is a Self-hosted Composer Repository Made with Laravel

Read article
Implementing The Repository Pattern in Laravel 5 image

Implementing The Repository Pattern in Laravel 5

Read article
Mock PHP Classes in Tests With the Double Library image

Mock PHP Classes in Tests With the Double Library

Read article
May 2018 Git Security Vulnerability image

May 2018 Git Security Vulnerability

Read article
Automated Localization of Laravel Projects with Localazy and GitHub Actions image

Automated Localization of Laravel Projects with Localazy and GitHub Actions

Read article
Deploy your PHP Codebase with Ansible and GitHub Actions image

Deploy your PHP Codebase with Ansible and GitHub Actions

Read article

PhpStorm

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

PhpStorm

NativePHP

The fastest way to build fully native apps for any platform (with AI or not)

NativePHP

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

Let your users buy domains without leaving your app

If your Laravel app gives users a site, a storefront, or a workspace, they’ll eventually want their own domain. The name.com API lets them search, register, and connect one without leaving your product. It’s a clean REST API, so Laravel’s HTTP client is all you need.

Let your users buy domains without leaving your app

Shift

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

Shift

Kirschbaum

Providing innovation and stability to ensure your web application succeeds.

Kirschbaum