jeishanul/laravel-repository-generator

A Laravel package to generate Repository pattern classes and auto-bind them.

Installs: 1

Dependents: 0

Suggesters: 0

Security: 0

Stars: 0

Watchers: 0

Forks: 0

Open Issues: 0

Type:laravel-package

pkg:composer/jeishanul/laravel-repository-generator

1.0.0 2025-11-29 05:50 UTC

This package is not auto-updated.

Last update: 2025-12-14 04:27:25 UTC


README

Latest Version on Packagist GitHub Code Style Action Total Downloads

A simple Laravel Artisan command to scaffold the Repository pattern: generates an Interface in app/Interfaces, a concrete Repository in app/Repositories, and auto-binds them in AppServiceProvider.php. Supports nested namespaces (e.g., Api\Admin\User).

Features

  • Generates CRUD-ready Interface and Repository classes.
  • Handles nested paths (e.g., app/Interfaces/Api/Admin/UserInterface.php).
  • Automatic IoC binding for dependency injection.
  • Overwrite protection with --force flag.
  • Compatible with Laravel 10, 11, and 12.

Installation

You can install the package via Composer:

composer require jeishanul/laravel-repository-generator

The package will auto-discover its service provider. If not, add it manually to config/app.php under providers:

'providers' => [
    // ...
    Jeishanul\RepositoryGenerator\RepositoryGeneratorServiceProvider::class,
],

Usage

Run the command to generate files for a given repository:

php artisan make:repository User

Or with nested namespaces:

php artisan make:repository Api\Admin\User

Options

  • --force: Overwrite existing files.

Generated Files

For php artisan make:repository Api\Admin\User:

  1. Interface: app/Interfaces/Api/Admin/UserInterface.php

    <?php
    
    namespace App\Interfaces\Api\Admin;
    
    interface UserInterface
    {
        /**
         * Get all users.
         */
        public function all();
    
        /**
         * Find a user by ID.
         */
        public function find($id);
    
        /**
         * Create a new user.
         */
        public function create(array $data);
    
        /**
         * Update a user.
         */
        public function update($id, array $data);
    
        /**
         * Delete a user.
         */
        public function delete($id);
    }
  2. Repository: app/Repositories/Api/Admin/UserRepository.php

    <?php
    
    namespace App\Repositories\Api\Admin;
    
    use App\Interfaces\Api\Admin\UserInterface;
    use App\Models\User;
    use Illuminate\Database\Eloquent\Collection;
    
    class UserRepository implements UserInterface
    {
        protected $model;
    
        public function __construct(User $model)
        {
            $this->model = $model;
        }
    
        public function all(): Collection
        {
            return $this->model->all();
        }
    
        public function find($id)
        {
            return $this->model->findOrFail($id);
        }
    
        public function create(array $data): User
        {
            return $this->model->create($data);
        }
    
        public function update($id, array $data): bool
        {
            $model = $this->find($id);
            return $model->update($data);
        }
    
        public function delete($id): bool
        {
            $model = $this->find($id);
            return $model->delete();
        }
    }
  3. Auto-Binding: Appended to app/Providers/AppServiceProvider.php in the register() method:

    public function register()
    {
        // ... existing code ...
    
        $this->app->bind(App\Interfaces\Api\Admin\UserInterface::class, App\Repositories\Api\Admin\UserRepository::class);
    }

Using in Controllers

Inject the interface for loose coupling:

<?php

namespace App\Http\Controllers;

use App\Interfaces\Api\Admin\UserInterface;

class UserController extends Controller
{
    protected $userRepo;

    public function __construct(UserInterface $userRepo)
    {
        $this->userRepo = $userRepo;
    }

    public function index()
    {
        return $this->userRepo->all();
    }

    public function store(Request $request)
    {
        return $this->userRepo->create($request->validated());
    }

    // ... other methods
}

Customization

  • Stubs: Override stubs by publishing them (future feature) or editing src/Stubs/ in your fork.
  • Model Path: Assumes flat models (e.g., App\Models\User). For nested models, update the stub's import.
  • Bindings: Uses AppServiceProvider for simplicity. For production, consider a dedicated RepositoryServiceProvider.

Testing

composer test

(Tests use PHPUnit; add your own for custom stubs.)

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Thank you for considering contributing! Please follow these steps:

  1. Fork the repo on GitHub.
  2. Create a feature branch (git checkout -b feature/new-feature).
  3. Commit changes (git commit -am 'Add new feature').
  4. Push to the branch (git push origin feature/new-feature).
  5. Open a Pull Request.

Security Vulnerabilities

If you discover a security vulnerability, please email shishirjeishanul@gmail.com instead of opening an issue.

License

The MIT License (MIT). Please see License File for more information.

Credits

Star this repo if it helps your project! Follow @jeishanul on GitHub for more packages.