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

sajadsdi/laravel-repository

Laravel repository pattern package to structure data access in your apps. Provides base repository classes, common CRUD methods, and an easy way to keep queries and persistence logic out of controllers and services for cleaner, testable code.

View on GitHub
Deep Wiki
Context7

Advanced Tools to use repository pattern for laravel eloquent models.

Frequently asked questions about Laravel Repository
How do I install and set up sajadsdi/laravel-repository in a Laravel 11 project?
Run `composer require sajadsdi/laravel-repository`, then extend the base `Repository` class for each Eloquent model. Define abstract methods like `getModelName()`, `getSearchable()`, `getFilterable()`, and `getSortable()` in your custom repository class to configure behavior. No additional configuration is needed for basic usage.
Does this package support Laravel 9.x or older versions?
The package officially supports Laravel 10 and 11 as of 2024. While it may work with Laravel 9.x, backward compatibility isn’t guaranteed, and you may need to adjust for breaking changes in newer Laravel versions. Always test thoroughly before migrating.
Can I use this package for complex queries like joins or subqueries?
Yes, the package supports advanced joins and query building. Use the `with()` method for eager loading relations or define custom query scopes in your repository. For complex logic, leverage Eloquent’s query builder directly within repository methods.
How does this package handle pagination and sorting?
Pagination is built-in via Laravel’s native pagination methods (e.g., `paginate()`). Sorting is configurable via `getSortable()` and applied dynamically using the `sortBy()` method. You can also chain multiple sorting rules for multi-column sorting.
Will repositories slow down my application? How can I optimize performance?
Repositories add minimal overhead for simple CRUD operations. For performance-critical queries, use caching via `remember()` or `forget()` methods. Avoid over-fetching data by leveraging Eloquent’s lazy loading and repository-level query optimization.
Can I integrate this package with Laravel’s events or observers?
Yes, the package supports event-driven hooks (e.g., `Creating`, `Updating`) for pre/post-operation logic. Replace model observers with repository events to centralize business logic. Events are triggered automatically when using repository CRUD methods.
How do I test repositories in PHPUnit? Can I mock database calls?
Repositories are designed for testability. Use Laravel’s mocking tools to replace the underlying Eloquent model with a fake implementation. For example, mock `User::query()` in your tests to isolate repository logic from the database.
What’s the best way to migrate from direct Eloquent calls to repositories?
Start by creating repositories for high-churn models (e.g., `User`, `Order`). Replace direct model calls in controllers/services incrementally. Use feature flags or deprecated methods to phase out old code. Audit your codebase first to identify dependencies on magic methods or global scopes.
Does this package work with Laravel Scout or non-Eloquent databases?
This package is optimized for Eloquent models and won’t work with non-Eloquent databases like MongoDB via Scout. For Scout, consider using a dedicated search package or implementing custom repository logic for search queries.
Are there alternatives to this package for implementing the repository pattern in Laravel?
Alternatives include `gloudemans/shoppinglist` (simpler), `spatie/laravel-query-builder` (query-focused), or rolling your own with interfaces and base classes. This package stands out for its built-in CRUD tools, dynamic method forwarding, and advanced filtering/search capabilities.
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.
codifyo/ts-generator-bundle
andydefer/laravel-cluster
testo/fiber
mintobit/jobqueue
a4sex/maintenance-bundle
a4sex/entity-date-update
a4sex/client-identifier
a4sex/base-utilites
a4sex/key-value-storage
a4sex/micro-status
chilldev/dependency-injection-extra
datinglibre/datinglibre-app-api
biberltd/corebundle
bricre/symfony-bundle-test
biberltd/logbundle
dominium/http-adapter-bundle
dominium/google-analytics
a4sex/auto-clean-entity
christhompsontldr/laravel-inky
spatie/mailcoach-vapor