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

Repositories Laravel Package

bosnadev/repositories

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. Installation:

    composer require bosnadev/repositories:0.*
    

    Publish the config (optional):

    php artisan vendor:publish --provider="Bosnadev\Repositories\RepositoriesServiceProvider"
    
  2. Define a Repository: Create a repository class extending Bosnadev\Repositories\Eloquent\Repository and implement model():

    namespace App\Repositories;
    
    use App\Models\User;
    use Bosnadev\Repositories\Eloquent\Repository;
    
    class UserRepository extends Repository
    {
        public function model()
        {
            return User::class;
        }
    }
    
  3. Register in Service Provider: Bind the repository in AppServiceProvider:

    $this->app->bind(
        \App\Repositories\UserRepository::class,
        function ($app) {
            return new \App\Repositories\UserRepository(new \App\Models\User);
        }
    );
    
  4. First Use Case: Inject the repository into a controller/service and use built-in methods:

    $users = $this->userRepository->all();
    $user = $this->userRepository->find(1);
    

Implementation Patterns

Core Workflows

  1. CRUD Operations: Use built-in methods for common operations:

    $this->userRepository->create(['name' => 'John']);
    $this->userRepository->update(1, ['name' => 'Jane']);
    $this->userRepository->delete(1);
    
  2. Query Scoping: Chain methods for complex queries:

    $activeUsers = $this->userRepository->scopeQuery(function ($query) {
        return $query->where('active', true);
    })->get();
    
  3. Pagination:

    $users = $this->userRepository->paginate(10);
    
  4. Eager Loading:

    $users = $this->userRepository->with('posts')->find(1);
    

Integration Tips

  • Dependency Injection: Prefer constructor injection for repositories:

    public function __construct(UserRepository $userRepository) {
        $this->userRepository = $userRepository;
    }
    
  • Service Layer: Use repositories in a service layer to decouple business logic from controllers.

  • Custom Methods: Extend repositories with domain-specific methods:

    public function findByEmail($email)
    {
        return $this->scopeQuery(function ($query) use ($email) {
            return $query->where('email', $email);
        })->first();
    }
    
  • Events: Trigger events post-create/update/delete:

    $this->userRepository->create(['name' => 'John'], true); // Fires events
    

Gotchas and Tips

Pitfalls

  1. Model Binding: Forgetting to bind the repository in the service provider will result in ClassNotFoundException.

  2. Model Method Conflict: Overriding model() incorrectly (e.g., returning an instance instead of a class name) will cause errors:

    // Wrong:
    public function model() { return new User; }
    
    // Correct:
    public function model() { return User::class; }
    
  3. Query Scope Overrides: Accidentally overriding scopeQuery() can break default query behavior.

  4. Event Handling: Events are disabled by default for create(), update(), and delete(). Pass true as the second argument to enable:

    $this->userRepository->create($data, true);
    

Debugging

  • Query Logging: Enable query logging in config/repositories.php:

    'logging' => true,
    

    Logs will appear in Laravel's default log channels.

  • Repository Methods: Use dd($this->userRepository->getMethodList()) to inspect available methods dynamically.

Config Quirks

  • Default Scope: Configure default scopes in config/repositories.php:

    'default_scopes' => [
        \App\Scopes\ActiveScope::class,
    ],
    
  • Model Events: Customize event names in the config:

    'events' => [
        'created' => 'user.created',
        'updated' => 'user.updated',
    ],
    

Extension Points

  1. Custom Scopes: Create reusable query scopes:

    namespace App\Scopes;
    
    use Bosnadev\Repositories\Contracts\Repository as RepositoryContract;
    
    class ActiveScope implements \Bosnadev\Repositories\Contracts\Scope
    {
        public function apply(RepositoryContract $repository, $model)
        {
            return $model->where('active', true);
        }
    }
    
  2. Repository Interfaces: Define interfaces for better type-hinting and testing:

    interface UserRepositoryInterface {
        public function findByEmail($email);
    }
    
  3. Repository Traits: Share common logic across repositories:

    trait SoftDeletesTrait {
        public function softDelete($id) {
            return $this->update($id, ['deleted_at' => now()]);
        }
    }
    
  4. Repository Decorators: Decorate repositories for cross-cutting concerns (e.g., logging, caching):

    class LoggingRepositoryDecorator implements \Bosnadev\Repositories\Contracts\Repository
    {
        protected $repository;
    
        public function __construct(UserRepository $repository) {
            $this->repository = $repository;
        }
    
        public function create(array $attributes, $fireEvents = false)
        {
            \Log::info('Creating user', $attributes);
            return $this->repository->create($attributes, $fireEvents);
        }
    }
    
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.
make-dev/orca
dmstr/symfony-system-resources-bundle
dmstr/symfony-job-queue-bundle
dmstr/openapi-json-schema-bundle
dmstr/keycloak-security-bundle
dmstr/doctrine-audit-log-bundle
dmstr/api-platform-utils-bundle
dmstr/api-configuration-bundle
chrisdev/ux-components
baks-dev/finances
emuniq/filament-browser-notifications
syriable/filament-translator
hungnm28/livewire-form
wenprise/eloquent
crudly/encrypted
fadion/bouncy
cuci/prototurk-sdk
gos/pubsub-router-bundle
cuci/prototurk-sdk-symfony
clementtalleu/easyadmin-markdown-bundle