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 Laravel Package

torann/laravel-repository

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. Installation:

    composer require torann/laravel-repository
    

    Publish the config (optional but recommended for customization):

    php artisan vendor:publish --provider="Torann\Repository\RepositoryServiceProvider"
    
  2. Define a Model and Repository:

    php artisan make:repository UserRepository --model=User
    

    This generates:

    • app/Repositories/UserRepository.php (interface)
    • app/Repositories/Eloquent/UserRepositoryEloquent.php (implementation)
  3. First Use Case: Inject the repository into a service or controller:

    use App\Repositories\UserRepository;
    
    class UserService {
        protected $users;
    
        public function __construct(UserRepository $users) {
            $this->users = $users;
        }
    
        public function getUser($id) {
            return $this->users->find($id);
        }
    }
    
  4. Key Files to Review:

    • config/repository.php (default configurations)
    • app/Providers/RepositoryServiceProvider.php (binding custom repositories)
    • app/Repositories/Eloquent/BaseRepositoryEloquent.php (base implementation)

Implementation Patterns

Core Workflows

1. CRUD Operations

// Create
$user = $this->users->create(['name' => 'John', 'email' => 'john@example.com']);

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

// Update
$user = $this->users->update(1, ['name' => 'Updated Name']);

// Delete
$this->users->delete(1);

2. Query Scoping

Use the scope* methods for reusable query constraints:

// Define in UserRepositoryEloquent
public function scopeActive($query) {
    return $query->where('active', true);
}

// Usage
$activeUsers = $this->users->active()->get();

3. Advanced Querying

Leverage Eloquent methods via the repository:

$recentUsers = $this->users->latest()->take(10)->get();
$searchResults = $this->users->where('name', 'like', '%John%')->get();

4. Transactions

Wrap operations in transactions:

$this->users->transaction(function ($repository) {
    $user = $repository->create(['name' => 'Alice']);
    $repository->create(['name' => 'Bob']);
    // Both or none are created
});

5. Events and Observers

Trigger model events via the repository:

$this->users->create(['name' => 'Event User']); // Triggers `created` event

Integration Tips

Dependency Injection

Bind custom repositories in RepositoryServiceProvider:

$this->app->bind(
    'App\Repositories\CustomUserRepository',
    function ($app) {
        return new \App\Repositories\Eloquent\CustomUserRepositoryEloquent(
            new \App\Models\User,
            $app['db']
        );
    }
);

API Resource Integration

Use repositories in API resources for consistent data shaping:

public function toArray($request) {
    return [
        'id' => $this->user->id,
        'name' => $this->user->name,
        // Use repository methods to fetch related data
        'posts' => $this->user->posts()->count(),
    ];
}

Testing

Mock repositories in tests:

$mock = Mockery::mock('overload:' . UserRepository::class);
$mock->shouldReceive('find')->andReturn($user);

Gotchas and Tips

Pitfalls

  1. Over-Querying: Avoid chaining multiple get() calls without constraints:

    // Bad: Loads all users, then filters in memory
    $activeUsers = $this->users->all()->filter(fn($u) => $u->active);
    
    // Good: Push filtering to the database
    $activeUsers = $this->users->active()->get();
    
  2. N+1 Queries: Use with() to eager-load relationships:

    $users = $this->users->with('posts')->get();
    
  3. Repository Bloat: Avoid putting business logic in repositories. Use services for complex workflows.

  4. Model Binding: Ensure your repository’s model matches the actual Eloquent model class.


Debugging

  1. Query Logging: Enable Laravel’s query logging to inspect generated queries:

    DB::enableQueryLog();
    $this->users->all();
    dd(DB::getQueryLog());
    
  2. Repository Method Not Found: If a method isn’t found, check:

    • The method exists in the Eloquent implementation (*RepositoryEloquent.php).
    • The method is properly proxied in the interface (*Repository.php).
  3. Transaction Rollbacks: If a transaction fails silently, wrap in a try-catch:

    try {
        $this->users->transaction(...);
    } catch (\Exception $e) {
        // Handle rollback
    }
    

Tips

  1. Custom Scopes: Extend the base repository to add reusable scopes:

    // app/Repositories/Eloquent/BaseRepositoryEloquent.php
    public function scopeSearch($query, $term) {
        return $query->where('name', 'like', "%{$term}%");
    }
    
  2. Repository Events: Listen for repository events (e.g., repository.created) in EventServiceProvider:

    protected $listen = [
        'repository.created' => [
           'App\Listeners\LogRepositoryCreation',
       ],
    ];
    
  3. Performance: Use cursor() for large datasets to reduce memory usage:

    foreach ($this->users->cursor() as $user) {
        // Process one user at a time
    }
    
  4. Soft Deletes: Enable soft deletes in the repository:

    public function __construct(Model $model) {
        parent::__construct($model);
        $this->model->usesSoftDeletes();
    }
    
  5. Caching: Cache repository results for read-heavy operations:

    $users = Cache::remember('users.all', now()->addHours(1), function () {
        return $this->users->all();
    });
    
  6. API Versioning: Use repositories to abstract versioned API logic:

    // v1/UserRepository.php
    // v2/UserRepository.php
    
  7. Repository Interfaces: Always implement interfaces to enforce contracts and enable mocking:

    class UserRepository implements UserRepositoryInterface {
        // ...
    }
    
  8. Mass Assignment: Use $fillable or $guarded in the repository’s create/update methods:

    public function create(array $attributes) {
        return $this->model->create(array_merge($this->getFillable(), $attributes));
    }
    
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.
elnasnato/laraliveui
labrodev/rest-sdk
sampaui/sampaui
babelqueue/php-sdk
facebook/capi-param-builder-php
babelqueue/symfony
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