PHPackages                             laravelplus/repository-pattern - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Framework](/categories/framework)
4. /
5. laravelplus/repository-pattern

ActiveLibrary[Framework](/categories/framework)

laravelplus/repository-pattern
==============================

Laravel repository-pattern

10PHPCI failing

Since Jun 18Pushed 11mo agoCompare

[ Source](https://github.com/LaravelPlus/repository-pattern)[ Packagist](https://packagist.org/packages/laravelplus/repository-pattern)[ RSS](/packages/laravelplus-repository-pattern/feed)WikiDiscussions Master Synced 1mo ago

READMEChangelogDependenciesVersions (1)Used By (0)

LaravelPlus Repository Pattern
==============================

[](#laravelplus-repository-pattern)

A Laravel package that helps you organize all your Eloquent queries for each model in dedicated repository classes, making your codebase more maintainable, testable, and clean. Centralize your data access logic in one spot for each model, following best practices for modern Laravel development.

[![Latest Version on Packagist](https://camo.githubusercontent.com/9e256bf65f9fa81995cb0f6d28c8dbb6dfb721f80ff69c4e941e6b7799971a31/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6c61726176656c706c75732f7265706f7369746f72792d7061747465726e2e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/laravelplus/repository-pattern)[![Total Downloads](https://camo.githubusercontent.com/5a5d3623558ed25e9f46722dc51e3b2e82519206aff1e2b795527677c578e803/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6c61726176656c706c75732f7265706f7369746f72792d7061747465726e2e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/laravelplus/repository-pattern)[![GitHub Actions](https://github.com/laravelplus/repository-pattern/actions/workflows/main.yml/badge.svg)](https://github.com/laravelplus/repository-pattern/actions/workflows/main.yml/badge.svg)

This is where your description should go. Try and limit it to a paragraph or two, and maybe throw in a mention of what PSRs you support to avoid any confusion with users and contributors.

Installation
------------

[](#installation)

You can install the package via composer:

```
composer require laravelplus/repository-pattern
```

Usage
-----

[](#usage)

```
// Usage description here
```

Repository Pattern Usage
------------------------

[](#repository-pattern-usage)

The repository pattern helps you keep all your Eloquent queries for a model in one place, making your codebase more organized and easier to maintain. With this package, you can create repositories in `app/Repositories` and inject them wherever you need.

### Example: `app/Repositories/UserRepository.php`

[](#example-apprepositoriesuserrepositoryphp)

```
namespace App\Repositories;

use App\Models\User;

class UserRepository
{
    public function all()
    {
        return User::all();
    }

    public function find($id)
    {
        return User::find($id);
    }

    public function create(array $data)
    {
        return User::create($data);
    }

    // Add more query methods as needed
}
```

### Using the Repository in a Controller

[](#using-the-repository-in-a-controller)

```
use App\Repositories\UserRepository;

class UserController extends Controller
{
    protected $users;

    public function __construct(UserRepository $users)
    {
        $this->users = $users;
    }

    public function index()
    {
        $users = $this->users->all();
        return view('users.index', compact('users'));
    }
}
```

> **Tip:** Place all your model queries in their respective repositories under `app/Repositories` to keep your code clean and maintainable.

### Testing

[](#testing)

```
composer test
```

### Code Style: Pint

[](#code-style-pint)

This package uses [Laravel Pint](https://laravel.com/docs/10.x/pint) for code style fixing. To automatically fix code style issues, run:

```
composer pint
```

### Static Analysis: PHPStan

[](#static-analysis-phpstan)

This package uses [PHPStan](https://phpstan.org/) for static analysis. To run PHPStan, use:

```
composer phpstan
```

### Changelog

[](#changelog)

Please see [CHANGELOG](CHANGELOG.md) for more information what has changed recently.

Contributing
------------

[](#contributing)

Please see [CONTRIBUTING](CONTRIBUTING.md) for details.

### Security

[](#security)

If you discover any security related issues, please email  instead of using the issue tracker.

Credits
-------

[](#credits)

- [Nejc Cotic](https://github.com/laravelplus)
- [All Contributors](../../contributors)

License
-------

[](#license)

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

Laravel Package Boilerplate
---------------------------

[](#laravel-package-boilerplate)

This package was generated using the [Laravel Package Boilerplate](https://laravelpackageboilerplate.com).

Modern Repository Example with Traits
-------------------------------------

[](#modern-repository-example-with-traits)

You can enhance your repositories by mixing in traits for extra features like soft deletes, caching, and multi-database support.

### Example: `app/Repositories/UserRepository.php`

[](#example-apprepositoriesuserrepositoryphp-1)

```
namespace App\Repositories;

use App\Models\User;
use Laravelplus\RepositoryPattern\BaseRepository;
use Laravelplus\RepositoryPattern\Traits\SoftDeletes;
use Laravelplus\RepositoryPattern\Traits\Cacheable;
use Laravelplus\RepositoryPattern\Traits\MultiDatabase;

class UserRepository extends BaseRepository implements \Laravelplus\RepositoryPattern\Contracts\MultiDatabaseInterface
{
    use SoftDeletes, Cacheable, MultiDatabase;

    protected static string $modelClass = User::class;
    // Optionally configure $table, $primaryKey, $connection, etc.
}
```

### Using Trait Methods

[](#using-trait-methods)

```
$userRepo = new UserRepository();
$userRepo->softDelete($userId); // Soft delete a user
$userRepo->restore($userId);    // Restore a soft-deleted user
$userRepo->cacheAll(30);        // Cache all users for 30 minutes
$userRepo->runOnConnection('mysql2', fn($db) => $db->table('users')->get());
```

Available Traits
----------------

[](#available-traits)

- `SoftDeletes`: Adds soft delete, restore, and onlyTrashed methods.
- `Cacheable`: Adds cacheAll for caching results.
- `Loggable`: Adds logAction for logging repository actions.
- `Eventable`: Adds fireEvent for dispatching events.
- `ValidatesData`: Adds validate for validating data before create/update.
- `Searchable`: Adds search for column-based LIKE search.
- `Sortable`: Adds sortBy for sorting results.
- `HasRelationships`: Adds withRelations for eager loading relationships.
- `MultiDatabase`: Adds runOnConnection and crossConnectionQuery for multi-database support.

Mix and match these traits in your repositories as needed!

Repository Generator Command
----------------------------

[](#repository-generator-command)

You can quickly generate repository classes using the built-in Artisan command:

### Simple Repository

[](#simple-repository)

Generate a basic repository (no traits or interface):

```
php artisan make:repository User
```

This uses the `stubs/repository.simple.stub` template.

### Repository with Traits and Interface

[](#repository-with-traits-and-interface)

Generate a repository with traits and/or an interface:

```
php artisan make:repository User --traits=SoftDeletes,Cacheable --interface=MultiDatabaseInterface
```

This uses the `stubs/repository.stub` template and will insert the specified traits and interface.

### Customizing Stubs

[](#customizing-stubs)

You can publish the stubs to your application and customize them as needed:

```
php artisan vendor:publish --tag=config
```

This will copy the stubs to your `stubs/` directory, where you can edit them to fit your project's needs.

###  Health Score

15

—

LowBetter than 3% of packages

Maintenance38

Infrequent updates — may be unmaintained

Popularity2

Limited adoption so far

Community2

Small or concentrated contributor base

Maturity14

Early-stage or recently created project

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

### Community

Maintainers

![](https://www.gravatar.com/avatar/4e4629de002c40aef796e5b320091892f0b7b35b62497260c52cef3eb721eed1?d=identicon)[Nejcc](/maintainers/Nejcc)

### Embed Badge

![Health badge](/badges/laravelplus-repository-pattern/health.svg)

```
[![Health](https://phpackages.com/badges/laravelplus-repository-pattern/health.svg)](https://phpackages.com/packages/laravelplus-repository-pattern)
```

###  Alternatives

[laravel/passport

Laravel Passport provides OAuth2 server support to Laravel.

3.4k85.0M532](/packages/laravel-passport)[nolimits4web/swiper

Most modern mobile touch slider and framework with hardware accelerated transitions

41.8k177.2k1](/packages/nolimits4web-swiper)[laravel/dusk

Laravel Dusk provides simple end-to-end testing and browser automation.

1.9k36.7M259](/packages/laravel-dusk)[laravel/prompts

Add beautiful and user-friendly forms to your command-line applications.

712181.8M596](/packages/laravel-prompts)[cakephp/chronos

A simple API extension for DateTime.

1.4k47.7M121](/packages/cakephp-chronos)[laravel/pail

Easily delve into your Laravel application's log files directly from the command line.

91545.3M590](/packages/laravel-pail)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
