cuongnx/laravel-repo-service-generator

Generate repository-service structure for Laravel with interface and binding

Installs: 8

Dependents: 0

Suggesters: 0

Security: 0

Stars: 0

Watchers: 0

Forks: 0

Open Issues: 0

pkg:composer/cuongnx/laravel-repo-service-generator

v1.0.1 2025-07-29 16:17 UTC

This package is auto-updated.

Last update: 2025-09-29 16:42:02 UTC


README

โœ… Generate clean Repository-Service structure with interfaces, bindings, and optional MongoDB support for Laravel 11 and 12+

This library provides a powerful artisan command set to generate and manage Repository-Service architecture with optional binding, base classes, and multi-model support. It helps you follow a clean, testable architecture in Laravel projects.

๐Ÿงพ Version Information

Library Version v1.0.0
Laravel ^11.0, ^12.0
PHP Version >= 8.1
MongoDB Support Optional (--type=m) via mongodb/laravel-mongodb

๐Ÿ“š Table of Contents

โš™๏ธ โœ… Features

  • Generate Repository, Service, Interface automatically.
  • Optional Model generation (Eloquent or MongoDB).
  • Auto-bind/unbind to AppServiceProvider.
  • Middleware-safe service usage.
  • Reversible file generation (remove struct).
  • Extendable base classes.
  • Fast CLI operations.

โš™๏ธ Installation

composer require cuongnx/laravel-repo-service-generator

๐Ÿ“ฆ For MongoDB support:

composer require mongodb/laravel-mongodb

๐Ÿ“ฆ Base Structure Generation

Create base repository/service interfaces and classes:

php artisan cuongnx:make-base

Options:

Flag Description
--f Overwrite existing files
--force Alias for --f

This command will generate the following files and structure:

app/
โ”œโ”€โ”€ Repositories/
โ”‚   โ”œโ”€โ”€ Contracts/
โ”‚   โ”‚   โ””โ”€โ”€ BaseRepositoryInterface.php
โ”‚   โ””โ”€โ”€ BaseRepository.php
โ””โ”€โ”€ Services/
    โ”œโ”€โ”€ Contracts/
    โ”‚   โ””โ”€โ”€ BaseServiceInterface.php
    โ””โ”€โ”€ BaseService.php

File Descriptions:

  • app/Repositories/Contracts/BaseRepositoryInterface.php
    โ†’ Interface that defines common methods for a repository.

  • app/Repositories/Eloquent/BaseRepository.php
    โ†’ Implements basic data access methods (CRUD, conditions, pagination...).

  • app/Services/Contracts/BaseServiceInterface.php
    โ†’ Interface that defines common methods for a service layer.

  • app/Services/BaseService.php
    โ†’ Implements business logic on top of the repository layer.

๐Ÿงฑ Create Full Structure (Model + Repo + Service)

php artisan cuongnx:make-struct Post

Options:

Flag Description
--model, --m Also generate the model class
--type= Model type: d = Eloquent (default), m = MongoDB
--no-bind Skip automatic binding in AppServiceProvider
--f, --force Overwrite existing files

๐Ÿ“Œ Example with MongoDB:

php artisan cuongnx:make-struct Product --m --type=m

This command will generate the following files and structure:

app/
โ”œโ”€โ”€ Models/
โ”‚   โ””โ”€โ”€ Post.php
โ”œโ”€โ”€ Repositories/
โ”‚   โ”œโ”€โ”€ Contracts/
โ”‚   โ”‚   โ””โ”€โ”€ PostRepositoryInterface.php
โ”‚   โ””โ”€โ”€ PostRepository.php
โ””โ”€โ”€ Services/
    โ”œโ”€โ”€ Contracts/
    โ”‚   โ””โ”€โ”€ PostServiceInterface.php
    โ””โ”€โ”€ PostService.php

File Descriptions:

  • app/Models/Post.php
    โ†’ The Eloquent or MongoDB model class for Post.

  • app/Repositories/Contracts/PostRepositoryInterface.php
    โ†’ Interface defining methods specific to the Post repository.

  • app/Repositories/PostRepository.php
    โ†’ Repository class implementing data access logic for Post.
    Extends BaseRepository and implements PostRepositoryInterface.

  • app/Services/Contracts/PostServiceInterface.php
    โ†’ Interface defining service-level business methods for Post.

  • app/Services/PostService.php
    โ†’ Service class implementing business logic related to Post.
    Extends BaseService and implements PostServiceInterface.

๐Ÿ“Œ If --no-bind is not provided, the following bindings will be added to AppServiceProvider:

$this->app->bind(
    \App\Repositories\Contracts\PostRepositoryInterface::class,
    \App\Repositories\PostRepository::class
);

$this->app->bind(
    \App\Services\Contracts\PostServiceInterface::class,
    \App\Services\PostService::class
);

๐Ÿงฑ Create Simple Service & Interface (Without implement BaseService)

php artisan cuongnx:make-service Custom

Options:

Flag Description
--no-bind Skip automatic binding in AppServiceProvider
--f, --force Overwrite existing files

This command will generate the following files and structure:

app/
โ””โ”€โ”€ Services/
    โ”œโ”€โ”€ Contracts/
    โ”‚   โ””โ”€โ”€ CustomServiceInterface.php
    โ””โ”€โ”€ CustomService.php

File Descriptions:

  • app/Services/Contracts/CustomServiceInterface.php
    โ†’ Defines custom methods for your Custom service logic.

  • app/Services/CustomService.php
    โ†’ Contains business logic related to Custom, implements CustomServiceInterface.

๐Ÿ”Œ Bindings

Bind both repository & service:
php artisan cuongnx:bind-model User

Options:

Flag Description
--only=repo Bind only repository
--only=service Bind only service

Bind individually:

php artisan cuongnx:bind-repo User
php artisan cuongnx:bind-service User

โŒ Unbind Bindings

```bash php artisan cuongnx:unbind-model User ```

Options:

Flag Description
--only=repo Unbind only repository
--only=service Unbind only service

Or directly:

php artisan cuongnx:unbind-repo User
php artisan cuongnx:unbind-service User

๐Ÿงน Remove Structures

Remove all (repo + service + optional model):
php artisan cuongnx:remove-struct Post --model

Options:

Flag Description
--model, -m Also remove model
--no-unbind Do not unbind from AppServiceProvider

Remove only service:

php artisan cuongnx:remove-service Post

๐Ÿงฉ BaseService Methods

All services extend `BaseService` and automatically gain access to these common data methods.

๐Ÿ” Read Methods

Method Description
getAll(array $relations = []) Get all records with optional relationships
get(?array $fields = null, array $relations = []) Get all records with selected fields & relationships
find($id, ?array $fields = null, array $relations = []) Find by ID
findBy(string $key, $value, ?array $fields = null, array $relations = []) Find a record by key-value
findByAttributes(array $conditions, ?array $fields = null, array $relations = []) Find a single record by multiple attributes
getBy(string $key, $value, ?array $fields = null, array $relations = []) Get multiple records by key-value
getByAttributes(array $conditions, ?array $fields = null, array $relations = []) Get multiple records by attributes
withTrashed(array $conditions = [], ?array $fields = null, array $relations = []) Get including soft-deleted
onlyTrashed(array $conditions = [], ?array $fields = null, array $relations = []) Get only soft-deleted

๐Ÿ“Š Paginate

Method Description
paginate(int $perPage = 15, array $conditions = [], ?array $fields = null, array $relations = []) Paginated list with filters and relationships

โœ… Existence Checks

Method Description
existsBy(string $field, $value): bool Check if a value exists for a field
existsByAttributes(array $conditions): bool Check existence by multiple attributes

๐Ÿ“ Create/Update

Method Description
create(array $data) Create a new record
update($id, array $data) Update by ID
updateFields($model, array $fields, array $except = []) Update specific fields on a model
createOrUpdate(array $attributes, array $values = [], array $checkFields = []) Create or update a record by match conditions

โŒ Delete / Restore

Method Description
delete($id) Soft delete by ID
deleteBy(array $conditions) Soft delete by conditions
restore($id): bool Restore soft-deleted record
forceDelete($id): bool Permanently delete record

๐Ÿง  Service Usage Example

use App\Services\UserService;

class PostService extends BaseService
{
    public function __construct(protected UserService $userService) {}

    protected function getRepository()
    {
        return $this->postRepo;
    }

    public function assignAuthor($postId, $userId)
    {
        $user = $this->userService->find($userId);
        $post = $this->find($postId);
        $post->author_id = $user->id;
        $post->save();

        return $post;
    }
}

Controller:

use App\Services\PostService;

class PostController extends Controller
{
    public function __construct(protected PostService $postService) {}

    public function index()
    {
        return $this->postService->getAll();
    }
}

๐Ÿ“ Folder Structure

app/
โ”œโ”€โ”€ Repositories/
โ”‚   โ”œโ”€โ”€ Contracts/
โ”‚   โ”‚   โ””โ”€โ”€ BaseRepositoryInterface.php
โ”‚   โ”‚   โ””โ”€โ”€ UserRepositoryInterface.php
โ”‚   โ”‚   โ””โ”€โ”€ PostRepositoryInterface.php
โ”‚   โ””โ”€โ”€ Eloquent/
โ”‚       โ””โ”€โ”€ BaseRepository.php
โ”‚       โ””โ”€โ”€ UserRepository.php
โ”‚       โ””โ”€โ”€ PostRepository.php
โ”œโ”€โ”€ Services/
โ”‚   โ”œโ”€โ”€ Contracts/
โ”‚   โ”‚   โ””โ”€โ”€ BaseServiceInterface.php
โ”‚   โ”‚   โ””โ”€โ”€ CustomServiceInterface.php
โ”‚   โ”‚   โ””โ”€โ”€ PostServiceInterface.php
โ”‚   โ”‚   โ””โ”€โ”€ BaseServiceInterface.php
โ”‚   โ”œโ”€โ”€ BaseService.php
โ”‚   โ””โ”€โ”€ CustomService.php
โ”‚   โ””โ”€โ”€ UserService.php
โ”‚   โ””โ”€โ”€ PostService.php
โ””โ”€โ”€ Models/
    โ””โ”€โ”€ User.php
    โ””โ”€โ”€ Post.php    

๐Ÿงฉ Extending BaseService

You can override or extend any of these methods in your custom service classes.

๐Ÿ’– Donate

If you find this package useful, feel free to support the development:

โ˜• Coffee & Support

๐Ÿฆ Bank (VIETQR)

QR Code Techcombank

Account Holder: NGUYEN XUAN CUONG
Account Number: 1368686856
Bank: Techcombank

๐Ÿ“ฌ Contact

๐Ÿ“ License