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

What are the benefits of using Repositories? image

What are the benefits of using Repositories?

Read article
OpenAI for Laravel image

OpenAI for Laravel

Read article
Summarize your pull requests in seconds with What The Diff image

Summarize your pull requests in seconds with What The Diff

Read article
Speed up your CI builds with Airdrop image

Speed up your CI builds with Airdrop

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

Deploy your PHP Codebase with Ansible and GitHub Actions

Read article
Sublime Merge image

Sublime Merge

Read article

Pushsilver

Pushsilver is simple invoicing for freelancers and small studios — send polished invoices, automate reminders, and get paid on time without the bloat.

Pushsilver

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

PhpStorm

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

PhpStorm

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

Harpoon: Next generation time tracking and invoicing

The next generation time-tracking and billing software that helps your agency plan and forecast a profitable future.

Harpoon: Next generation time tracking and invoicing

Kirschbaum

Providing innovation and stability to ensure your web application succeeds.

Kirschbaum