PHPackages                             ysm/laravel-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. [Utility &amp; Helpers](/categories/utility)
4. /
5. ysm/laravel-repository-pattern

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

ysm/laravel-repository-pattern
==============================

A Laravel package implementing the Repository Design Pattern with Service Layer

1.0.1(1y ago)826MITPHPPHP ^8.0

Since Apr 21Pushed 10mo ago1 watchersCompare

[ Source](https://github.com/DevYSM/laravel-repository-pattern)[ Packagist](https://packagist.org/packages/ysm/laravel-repository-pattern)[ RSS](/packages/ysm-laravel-repository-pattern/feed)WikiDiscussions main Synced 1mo ago

READMEChangelogDependencies (1)Versions (3)Used By (0)

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

[](#laravel-repository-pattern)

A lightweight Laravel package implementing the Repository Design Pattern with a Service Layer for clean database operations, supporting Eloquent relationships, sorting, and optional soft deletes. Compatible with Laravel 8.0 to 12.0 and PHP 8.0+.

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

[](#installation)

1. Install via Composer:

    ```
    composer require ysm/laravel-repository-pattern
    ```
2. Publish configuration:

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

Configuration
-------------

[](#configuration)

Edit `config/repository-pattern.php` to manually define model-to-repository bindings:

```
return [
    'bindings' => [
        \App\Models\User::class => \App\Repositories\UserRepository::class,
    ],
];
```

Artisan Commands
----------------

[](#artisan-commands)

- **Generate a repository**:

    ```
    php artisan ysm:repository ModelName [--soft-deletes]
    ```
- **Generate a service**:

    ```
    php artisan ysm:service ModelName [--soft-deletes]
    ```
- **Generate a controller**:

    ```
    php artisan ysm:controller ModelName [--type=api|web] [--dir=CustomDir] [--soft-deletes] [--with-routes] [--with-rs]
    --type=api|web: Specify controller type (default: api)
    --dir=CustomDir: Custom controller and route directory (e.g., Api\Admin\v1)
    --soft-deletes: Include soft delete methods
    --with-routes: Generate and bind route file
    --with-rs: Generate repository and service
    ```

Methods
-------

[](#methods)

### RepositoryInterface / BaseRepository

[](#repositoryinterface--baserepository)

- `getAll(array $relations = [], string $orderBy = 'created_at', string $orderDir = 'DESC')`
    - Get all records with optional relationships and sorting.
- `paginate(int $perPage = 15, array $relations = [], string $orderBy = 'created_at', string $orderDir = 'DESC')`
    - Get paginated records with optional relationships and sorting.
- `show($value, string $column = 'id')`
    - Get a record by value and column.
- `create(array $data)`
    - Create a new record.
- `update($id, array $data)`
    - Update a record by ID.
- `delete($id)`
    - Delete a record by ID (hard delete).
- `with(array $relations)`
    - Load relationships for the query.
- `attach($id, string $relation, array $relatedIds)`
    - Attach IDs to a many-to-many relationship.
- `detach($id, string $relation, array $relatedIds)`
    - Detach IDs from a many-to-many relationship.
- `sync($id, string $relation, array $relatedIds)`
    - Sync IDs for a many-to-many relationship.
- `whereHas(string $relation, Closure $callback)`
    - Query records based on a relationship.

### BaseService

[](#baseservice)

- Mirrors all `RepositoryInterface` methods, delegating to the repository.
- `getRepository()`
    - Get the underlying repository.

### InteractsRepoWithSoftDeletes (Repository Trait)

[](#interactsrepowithsoftdeletes-repository-trait)

- `softDelete($id)`
    - Soft delete a record by ID.
- `permanentlyDelete($id)`
    - Permanently delete a soft-deleted record.
- `restore($id)`
    - Restore a soft-deleted record.

### InteractsServiceWithSoftDeletes (Service Trait)

[](#interactsservicewithsoftdeletes-service-trait)

- `softDelete($id)`
    - Soft delete a record via repository.
- `permanentlyDelete($id)`
    - Permanently delete a soft-deleted record via repository.
- `restore($id)`
    - Restore a soft-deleted record via repository.

Notes
-----

[](#notes)

- **Compatibility**: Requires Laravel 8.0–12.0 and PHP 8.0+. Ensure your project meets these requirements.
- **Bindings**: Model-to-repository bindings must be manually added to `config/repository-pattern.php`. No default binding exists for unbound models; consider adding a default repository or exception.
- **Controller Directory**: Use `--dir=CustomDir` to place controllers in `App\Http\Controllers\CustomDir` ( e.g., `Api\Admin\v1`) and routes in `routes/CustomDir/`.
- **Route Generation**: The `--with-routes` flag for `ysm:controller` generates a route file in `routes/api/`, `routes/web/`, or `routes/CustomDir/` (e.g., `routes/Api/Admin/v1/users.php`) and binds it in `RouteServiceProvider`. Outputs “🎉 Routes created successfully!”.
- **Repository and Service Generation**: The `--with-rs` flag for `ysm:controller` generates corresponding repository and service files, respecting `--soft-deletes`.
- **Controller Overwrite**: The `ysm:controller` command overwrites existing controllers, displaying “🎉 Controller \[Name\] (type) overwritten successfully.” or “🎉 Controller \[Name\] (type) created successfully.”
- **Repository Overwrite**: The `ysm:repository` command overwrites existing repositories, displaying “Repository \[Name\] overwritten successfully.” or “Repository \[Name\] created successfully.”

License
-------

[](#license)

MIT

###  Health Score

30

—

LowBetter than 65% of packages

Maintenance51

Moderate activity, may be stable

Popularity12

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity43

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

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.

###  Release Activity

Cadence

Every ~0 days

Total

2

Last Release

384d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/94ad099ac29460710c50f3c27489ebd2e8ee24d3cd5bfbaf040cd5d4a3a2dd1a?d=identicon)[Yassen Sayed](/maintainers/Yassen%20Sayed)

---

Top Contributors

[![DevYSM](https://avatars.githubusercontent.com/u/59166666?v=4)](https://github.com/DevYSM "DevYSM (20 commits)")

---

Tags

phplaravelgeneratorcommandrepository patternrepositorytraits

### Embed Badge

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

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

###  Alternatives

[prevailexcel/laravel-action-service-trait

A simple Laravel package to create actions, traits and services using artisan commands

143.0k](/packages/prevailexcel-laravel-action-service-trait)

PHPackages © 2026

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