Published on September 05 Aug 25

Clean Up Your Laravel Code with ysm-laravel-repository-pattern

1 month ago 73

Clean Up Your Laravel Code with ysm/laravel-repository-pattern

If you've worked with Laravel for a while, you’ve probably felt the pain of bloated controllers, duplicated logic across services, and messy query code that’s hard to test or maintain. It starts simple, but as your project grows, so does the chaos.

That’s where ysm/laravel-repository-pattern comes in — a lightweight Laravel package that gives you a ready-to-use Repository & Service Layer, built with developer experience in mind.


✨ What Does It Solve?

Laravel doesn’t come with the Repository pattern out of the box. And while it's not always needed, the moment your project grows or requires clean abstractions, it becomes essential.

This package gives you:

  • A structured Repository & Service pattern.

  • Optional soft delete support.

  • Auto-generated API or Web controllers, complete with route files.

  • Built-in Eloquent relationship handling.

  • CLI generators to build repositories, services, and controllers in seconds.

And it works out of the box with Laravel 8 → 12 and PHP 8+.


⚙️ Installation

Install the package via Composer:

composer require ysm/laravel-repository-pattern

Then publish the configuration:

php artisan vendor:publish --tag=config

This will create a config/repository-pattern.php file where you define model-repository bindings.


🧩 Configuration

Add model-to-repository bindings in your config file:

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

This tells the service layer which repository to inject when you call your services.


🛠️ Artisan Commands

This package shines with its generators. Run a single command to scaffold everything you need.

✅ Generate a Repository

php artisan ysm:repository User

Add --soft-deletes if your model uses soft deletes.

✅ Generate a Service

php artisan ysm:service User

Also supports --soft-deletes.

✅ Generate a Controller

php artisan ysm:controller User --type=api --with-routes --with-rs

Available options:

Option

Description

`--type=api

web`

--dir=Admin\v1

Custom controller namespace and route path

--with-routes

Auto-generate and bind a route file

--with-rs

Also generate the repository and service

--soft-deletes

Add soft delete methods in the generated class

Example:

php artisan ysm:controller Product --dir=Api\Admin\v1 --with-rs --with-routes --soft-deletes

✅ This generates:

  • ProductController at App\Http\Controllers\Api\Admin\v1

  • Routes at routes/Api/Admin/v1/products.php

  • Repository & Service with soft delete support


🧰 Built-in Methods

Here are just a few of the built-in methods you get by default:

BaseRepository

  • getAll(): List all records with optional sorting and relations

  • paginate(): Get paginated results

  • show($id): Find a record

  • create($data): Store a new record

  • update($id, $data): Update a record

  • delete($id): Hard delete

  • attach, detach, sync: Manage many-to-many relations

  • whereHas($relation, $callback): Query via relation

BaseService

  • Mirrors all Repository methods

  • getRepository(): Access the repository manually

Soft Delete Support

If you generate with --soft-deletes, you also get:

  • softDelete($id)

  • permanentlyDelete($id)

  • restore($id)

In both the repository and the service.


💡 Why Use This?

  • 🔄 DRY: Avoid writing the same query logic across your app.

  • 🧪 Testable: Unit test services and repositories independently.

  • 🧼 Clean: Keep your controllers focused on just request/response.

  • 🚀 Fast: Scaffold full layers in seconds, not hours.


📝 Notes

  • Compatible with Laravel 8 → 12 and PHP 8.0+

  • You must bind each model to its repository in the config file.

  • Routes are auto-bound in RouteServiceProvider if --with-routes is used.

  • Existing files will be overwritten with a clear success message.


🪪 License

MIT — free for personal and commercial use.


🔗 Get Started

🔗 GitHub: https://github.com/DevYSM/laravel-repository-pattern
📦 Packagist: ysm/laravel-repository-pattern


🙋‍♂️ Final Thoughts

This package was built to remove the friction of writing boilerplate logic for every model. If you're tired of messy controllers or want to scale your Laravel apps cleanly, give it a try.

Pull requests and contributions are welcome — and if you like it, consider giving it a ⭐️ on GitHub!

#JavaScript #jQuery #laravel #php #elasticsearch #mongodb #mysql #wordpress #html #css #bootstrap #php8 #ui-development #tailwindcss #postgresql #redis #docker #openai #codewithyassen #ysm #reposatory #repository