PHPackages                             gohari/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. [Testing &amp; Quality](/categories/testing)
4. /
5. gohari/repository-pattern

ActiveLibrary[Testing &amp; Quality](/categories/testing)

gohari/repository-pattern
=========================

A Laravel package to generate and use clean repository pattern classes with Artisan commands.

v1.1.0(1w ago)55MITPHPPHP ^8.1|^8.2|^8.3|^8.4CI passing

Since Jun 17Pushed 3d ago1 watchersCompare

[ Source](https://github.com/mohammadrezagohari/RepositoryPattern)[ Packagist](https://packagist.org/packages/gohari/repository-pattern)[ RSS](/packages/gohari-repository-pattern/feed)WikiDiscussions main Synced today

READMEChangelog (1)Dependencies (7)Versions (9)Used By (0)

Gohari Laravel Repository Pattern
=================================

[](#gohari-laravel-repository-pattern)

Build clean repositories for Laravel models with one Artisan command.

`gohari/repository-pattern` provides a reusable `BaseRepository`, a matching `BaseRepositoryInterface`, and a generator command that creates repository classes inside the Laravel application that installs your package.

Current release: `v1.1.0`

Documentation: [mohammadrezagohari.github.io/RepositoryPattern](https://mohammadrezagohari.github.io/RepositoryPattern/)

Features
--------

[](#features)

- Artisan repository and service layer generator
- Automatic Laravel package discovery
- Automatic model creation for missing `App\Models\...` classes
- Repository and interface stubs
- Custom output path support
- Optional automatic interface binding
- Optional Service, ServiceInterface, DTO, and config generation
- Relationship loading, sorting, and soft delete helpers
- Shared base methods for common Eloquent operations
- Backward-compatible method aliases like `insertData`, `updateItem`, and `deleteData`
- PHPUnit and Orchestra Testbench coverage for package behavior

Requirements
------------

[](#requirements)

- PHP `^8.3`
- Laravel `^10.0`, `^11.0`, `^12.0`, or `^13.0`

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

[](#installation)

Install the package with Composer:

```
composer require gohari/repository-pattern
```

Laravel will discover the service provider automatically:

```
Gohari\RepositoryPattern\RepositoryPatternServiceProvider::class
```

If package discovery is disabled in your app, register the provider manually in `config/app.php`:

```
'providers' => [
    Gohari\RepositoryPattern\RepositoryPatternServiceProvider::class,
],
```

Publish the config or generator stubs when you want to customize package defaults:

```
php artisan vendor:publish --tag=repository-pattern-config
php artisan vendor:publish --tag=repository-pattern-stubs
```

Published stubs live in:

```
resources/stubs/vendor/repository-pattern

```

Quick Start
-----------

[](#quick-start)

Create a repository for a model:

```
php artisan repository:make User --model=User
```

This creates:

```
app/Repositories/UserRepository.php
app/Repositories/Contracts/UserRepositoryInterface.php

```

If `App\Models\User` does not exist, the package will create it with Laravel's `make:model` command.

Command Usage
-------------

[](#command-usage)

```
php artisan repository:make {name} --model={model}
```

Arguments and options:

NameRequiredDescription`name`YesRepository name. `User` and `UserRepository` both generate `UserRepository`.`--model`NoModel class name or FQCN. Defaults to the repository name.`--path`NoOutput directory. Defaults to `app/Repositories`.`--interface-path`NoRepository interface output directory. Defaults to `app/Repositories/Contracts`.`--force`NoOverwrite existing repository files.`--bind`NoBind the generated interface to the repository in `App\Providers\RepositoryServiceProvider`.`--service`NoGenerate Service, ServiceInterface, DTO, and config alongside the repository.`--service-path`NoService output directory. Defaults to `app/Services`.`--dto`NoGenerate only the DTO alongside the repository.`--dto-path`NoDTO output directory. Defaults to `app/DTOs`.`--config`NoCopy `config/repository-pattern.php` into the application.Examples:

```
php artisan repository:make User --model=User
php artisan repository:make UserRepository --model=User
php artisan repository:make Product --model="App\Models\Product"
php artisan repository:make Admin/User --model=User
php artisan repository:make User --model=User --service
```

Use a custom output path:

```
php artisan repository:make Product --model=Product --path=app/Domain/Repositories
```

Overwrite existing files:

```
php artisan repository:make Product --model=Product --force
```

Create files and register the interface binding:

```
php artisan repository:make User --model=User --bind
```

Generate repository and service layers together:

```
php artisan repository:make User --model=User --service --bind
```

For fully-qualified model classes outside `App\Models`, the command will warn you if the model file is missing, but it will not generate it automatically.

Generated Repository
--------------------

[](#generated-repository)

For this command:

```
php artisan repository:make User --model=User
```

The package generates:

```
