Weave 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 Make Repository Laravel Package

jason-guru/laravel-make-repository

Laravel package adding Artisan “make:repository” scaffolding for the repository pattern. Quickly generate repository interfaces and implementations, keeping controllers slimmer and data access consistent and testable. Easy to install and customize for your app structure.

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. Installation:

    composer require jason-guru/laravel-make-repository
    

    Publish the package config (if needed):

    php artisan vendor:publish --provider="JasonGuru\LaravelMakeRepository\MakeRepositoryServiceProvider"
    
  2. First Use Case: Generate a repository for a model (e.g., User):

    php artisan make:repository User
    

    This creates:

    • app/Repositories/UserRepository.php (interface)
    • app/Repositories/Eloquent/UserRepositoryEloquent.php (implementation)
    • Optional: --migration flag to scaffold a migration.
  3. Where to Look First:

    • Check config/make-repository.php for customization (e.g., paths, stubs).
    • Review app/Repositories/Eloquent/ for generated implementations.
    • Default stubs are in vendor/jason-guru/laravel-make-repository/src/stubs/.

Implementation Patterns

Core Workflows

  1. Repository Generation:

    php artisan make:repository Post --migration --model=Post
    
    • --model: Specify an existing model (default: same name as repository).
    • --migration: Auto-generate a migration file.
  2. Customizing Stubs: Override default stubs by publishing them:

    php artisan vendor:publish --tag=make-repository-stubs
    

    Edit stubs in resources/stubs/ (e.g., repository.stub, eloquent.stub).

  3. Integration with Existing Code:

    • Dependency Injection: Bind the repository in AppServiceProvider:
      $this->app->bind(
          \App\Repositories\UserRepository::class,
          \App\Repositories\Eloquent\UserRepositoryEloquent::class
      );
      
    • Usage in Services:
      public function __construct(UserRepository $userRepo) {
          $this->userRepo = $userRepo;
      }
      
  4. Testing:

    • Mock repositories in tests:
      $this->mock(UserRepository::class)->shouldReceive('find')->andReturn($user);
      

Advanced Patterns

  • Multiple Implementations: Generate a JsonRepository or ApiRepository by extending the base command:

    php artisan make:repository User --type=json
    

    (Requires custom stubs or package extension.)

  • Scoped Repositories: Use traits or interfaces to define domain-specific methods (e.g., ActiveUserRepository).

  • Repository Events: Dispatch events in repository methods (e.g., UserCreated, UserUpdated).


Gotchas and Tips

Common Pitfalls

  1. Stub Path Issues:

    • If stubs aren’t found, verify make-repository.stubs path in config or republish stubs.
    • Default path: resources/stubs/ (create if missing).
  2. Model Not Found:

    • Ensure the --model flag matches an existing model or generate the model first:
      php artisan make:model Post
      
  3. Migration Conflicts:

    • The --migration flag creates a basic migration. Review it for conflicts with existing tables.
  4. Namespace Collisions:

    • Customize the namespace in config/make-repository.php to avoid clashes (e.g., Repositories\Custom\).

Debugging Tips

  • Command Errors: Run with -v for verbose output:

    php artisan make:repository User -v
    

    Check for missing dependencies (e.g., laravel/framework version compatibility).

  • Stub Syntax Errors: Validate stub files for PHP syntax or variable placeholders (e.g., {{namespace}}).

Extension Points

  1. Custom Repository Types: Extend the MakeRepositoryCommand to add new types (e.g., GraphQLRepository):

    // app/Console/Commands/MakeGraphQLRepository.php
    use JasonGuru\LaravelMakeRepository\MakeRepositoryCommand;
    
    class MakeGraphQLRepository extends MakeRepositoryCommand {
        protected $type = 'graphql';
        // Override stub paths/methods
    }
    
  2. Dynamic Method Generation: Use the addMethod helper in stubs to inject custom methods:

    {% for method in methods %}
    public function {{ method.name }}() {
        // Logic here
    }
    {% endfor %}
    
  3. Post-Generation Hooks: Add logic after generation in MakeRepositoryServiceProvider:

    public function boot() {
        MakeRepository::creating(function ($repository) {
            // Modify $repository before generation
        });
    }
    

Configuration Quirks

  • Path Customization: Override default paths in config/make-repository.php:
    'paths' => [
        'repository' => 'app/Repositories/Custom/',
        'eloquent'   => 'app/Repositories/Eloquent/',
    ],
    
  • Stub Variables: Use Twig variables in stubs (e.g., {{repositoryClass}}, {{modelClass}}). Undefined variables cause silent failures.
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.
mehedimi/laravel-amp-mail
happones/laravel-vue-i18n-generator
jwerd/inertia-table
siberfx/flysystem-wasabi
hollyit/token-replace
ippanelcom/laravel-sdk
adachsoft/changelog-linter
adachsoft/collection
payerurl/telegrambot-crypto-payment-instant-payout
dskripchenko/laravel-hybrid-search
riorizkyrainey/lara-ced
mehedimi/static-vel
zharp/zharp
nonsapiens/bigquery-model-sync
lgu-bayambang-ict-office/lgu-laravel-development-standards
andydefer/laravel-support
se7enxweb/flysystem
lampwick/laravel
jeslage/statamic-emojis
micahdshackelford/rmap-laravel