Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Laravel Repository Generator Laravel Package

jeishanul/laravel-repository-generator

Artisan generator for Laravel’s Repository pattern. Creates CRUD-ready interface and repository classes (supports nested namespaces), writes them to app/Interfaces and app/Repositories, and auto-binds them in AppServiceProvider. Laravel 10–12, --force overwrite.

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Steps

  1. Installation:

    composer require jeishanul/laravel-repository-generator
    

    No additional configuration is needed—auto-discovery handles the rest.

  2. First Use Case: Generate a repository for a User model in the default namespace:

    php artisan make:repository User
    

    This creates:

    • app/Interfaces/UserInterface.php (CRUD-ready interface)
    • app/Repositories/UserRepository.php (concrete implementation)
    • Auto-binds the interface to the repository in AppServiceProvider.
  3. Where to Look First:

    • Generated Files: Check app/Interfaces/ and app/Repositories/ for structure.
    • Service Provider: Verify bindings in AppServiceProvider.php (auto-generated).
    • Artisan Command: Run php artisan to see the full command syntax.

Implementation Patterns

Core Workflow

  1. Repository Generation:

    php artisan make:repository [name] [--force] [--namespace=Namespace]
    
    • Example for nested namespace (e.g., Api/Admin):
      php artisan make:repository User --namespace=Api/Admin
      
      Generates:
      • app/Interfaces/Api/Admin/UserInterface.php
      • app/Repositories/Api/Admin/UserRepository.php
  2. Integration with Controllers: Inject the interface into controllers (auto-resolved via IoC):

    use App\Interfaces\UserInterface;
    
    class UserController extends Controller {
        public function __construct(private UserInterface $userRepository) {}
    }
    
  3. Extending Interfaces: Add custom methods to the interface and implement them in the repository:

    // app/Interfaces/UserInterface.php
    public function getActiveUsers(): Collection;
    
    // app/Repositories/UserRepository.php
    public function getActiveUsers(): Collection {
        return User::active()->get();
    }
    

Best Practices

  • Namespace Consistency: Align repository namespaces with feature modules (e.g., Api/Admin for admin APIs).
  • Interface-First: Always extend the generated interface for new features.
  • Testing: Mock interfaces in tests (e.g., UserInterface for unit tests).

Gotchas and Tips

Pitfalls

  1. Overwrite Protection:

    • The command refuses to overwrite existing files by default. Use --force to bypass:
      php artisan make:repository User --force
      
    • Tip: Check for conflicts manually before forcing.
  2. Namespace Collisions:

    • Avoid duplicate namespaces (e.g., User in both root and Api/Admin). Use unique names:
      php artisan make:repository AdminUser --namespace=Api/Admin
      
  3. Binding Conflicts:

    • If manually binding repositories in AppServiceProvider, ensure they don’t clash with auto-generated bindings. Delete old bindings if regenerating.

Debugging

  • Missing Bindings:

    • Verify the service provider is registered (check config/app.php).
    • Run php artisan config:clear if bindings appear missing.
  • File Permissions:

    • Ensure Laravel can write to app/Interfaces/ and app/Repositories/. Set permissions if needed:
      chmod -R 755 app
      

Extension Points

  1. Custom Templates:

    • Override the default stubs by publishing them:
      php artisan vendor:publish --tag=repository-generator-stubs
      
    • Modify resources/stubs/repository.stub and resources/stubs/interface.stub.
  2. Additional Methods:

  3. Laravel Version Quirks:

    • Test on Laravel 10/11/12. If issues arise, check the GitHub issues or roll back to a stable version.

Pro Tips

  • Alias Interfaces: Use traits or aliases to reduce boilerplate:
    trait UsesUserRepository {
        public function __construct(private UserInterface $userRepo) {}
    }
    
  • Repository Layers: Combine with packages like spatie/laravel-query-builder for advanced query methods in repositories.
  • CI/CD: Add the command to your deployment pipeline to enforce repository patterns:
    # .github/workflows/laravel.yml
    - run: php artisan make:repository User --force
    
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
hamzi/corewatch
minionfactory/raw-hydrator
hexters/coinpayment
rjcodes/rjcms
act-training/laravel-permissions-manager
alimarchal/laravel-chart-of-accounts
babenkoivan/elastic-scout-driver
mkwebdesign/filament-watchdog-v5
renatomarinho/laravel-page-speed
zedmagdy/filament-business-hours
renatovdemoura/blade-elements-ui
devgeek/beacon-admin
benjamin-rqt/data-watcher-bundle
atriumphp/atrium
sandermuller/package-boost-laravel
sandermuller/boost-skills
redaxo/core
yusufgenc/filament-api-forge
l3aro/rating-star-for-filament
leek/filament-subtenant-scope