Download the PHP package darwinnatha/laravel-repository-pattern without Composer
On this page you can find all versions of the php package darwinnatha/laravel-repository-pattern. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download darwinnatha/laravel-repository-pattern
More information about darwinnatha/laravel-repository-pattern
Files in darwinnatha/laravel-repository-pattern
Package laravel-repository-pattern
Short Description An elegant and flexible Repository Architecture package for Eloquent models in Laravel.
License MIT
Informations about the package laravel-repository-pattern
Laravel Repository Architecture Package
A modern, robust, and type-safe Repository Pattern implementation for Laravel applications. Designed for API-first architecture, this package decouples your data layer, standardizes URL filtering/eager-loading, and enforces clean separation of concerns.
Key Features
- Zero-Config Auto-Binding: Interface contracts are automatically bound to Eloquent implementations in Laravel's Service Container.
- Artisan Generator CLI: Instantly scaffold Repository contracts and implementations using
php artisan make:repository. - Declarative API Querying: Configure HTTP
filters,includes,sorts, and eager-loadedrelationsdirectly in the constructor. - Native Pagination: Seamlessly switch between collections and paginated results (
['paginate' => 15]). - Type-Safety & Generics: Full PHPStan / Larastan support (
@template TModel of Model) for perfect IDE autocomplete. - Event Bypassing: Execute write/delete operations with or without triggering Eloquent model events.
- Full Soft Delete Support: Unified handling for soft-deleted and trashed records (
retrieve,collect,restore,forceDelete).
Installation
Install the package via Composer:
Quick Start & CLI Usage
Generate a contract interface and its Eloquent implementation in one command:
Generates:
app/Repositories/Contracts/CustomerRepository.phpapp/Repositories/Eloquent/CustomerRepositoryEloquent.php
1. Custom Methods (Contract vs Implementation)
To ensure strict typing and allow your controllers to call specific methods directly via dependency injection, any custom method added to the Eloquent Repository must first be defined in its Interface (Contract).
Step 1: Define the Method in the Contract (CustomerRepository.php)
Step 2: Implement the Method in Eloquent (CustomerRepositoryEloquent.php)
Alternative with pagination:
Step 3: Direct Call from Controller
The interface CustomerRepository provides direct access to autocompletion for standard and custom methods:
Declarative API Querying (Pagination, Filters, Includes, Sorts & Relations)
Instead of manually building query builders, configure allowed API query parameters directly within the repository's constructor:
HTTP Query Parameter Usage
Once configured in your repository, frontend clients and API consumers can dynamically filter, include relationships, and sort data straight from the URL parameters:
1. Pagination (?paginate & ?page)
Pagination is automatically handled by the all() and trashed() methods of the repository when the paginate parameter is passed.
API clients can control the page size and navigate between pages via native Laravel URL query parameters:
2. Filtering (?filter[...])
Filter resources using exact matches, partial string searches, or model scopes:
3. Including Relationships (?include=...)
Dynamically eager-load allowed relationships specified in the 'includes' array:
4. Sorting (?sort=...)
Sort results dynamically using attributes defined in the 'sorts' array (prefix with - for descending order):
5. Combining Query Parameters
Combine pagination, filtering, eager-loading, and sorting in a single request:
Architectural Guidelines & Best Practices
1. Auto Eager-Loaded Relations (relations)
The 'relations' array automatically appends relations to every query executed by the repository (via $query->with(...)). Use this for core data that the model always depends on (e.g., profile or credential).
2. Controller vs. Service Layer Separation (CQRS / Actions)
- In Controllers: Repositories should be injected to handle HTTP requests, pagination, and API parameter parsing. If an API request attempts to use unregistered query filters or relationships, an exception will be raised, keeping your API strict and secure.
- In Services / Actions / Commands (CQRS): For complex business logic, cross-repository operations, or background jobs, encapsulate repository calls inside dedicated Service Classes, Actions, or Query Handlers rather than chaining multiple repositories inside a single controller.
Complete API Reference
| Method | Parameters | Return Type | Description |
|---|---|---|---|
all() |
array $queries = [] |
Collection\|Paginator |
Retrieves all models. Supports ['paginate' => 15]. |
trashed() |
array $queries = [] |
Collection\|Paginator |
Retrieves all soft-deleted models. Supports ['paginate' => 15]. |
retrieve() |
string\|int $id, bool $withTrashed = false |
?Model |
Finds a single model by primary key. |
collect() |
array $ids, bool $withTrashed = false |
Collection |
Retrieves models matching an array of IDs. |
where() |
array $conditions |
Collection |
Retrieves models matching a condition array. |
whereFirst() |
array $conditions |
?Model |
Retrieves first matching model or null. |
whereFirstOrFail() |
array $conditions |
Model |
Retrieves first matching model or throws 404. |
create() |
array $attributes, bool $withoutEvents = false |
?Model |
Creates a model instance. |
update() |
string\|int\|Model $id, array $attributes, bool $withoutEvents = false |
Model |
Updates an existing model. |
createOrUpdate() |
array $conditions, array $attributes, bool $withoutEvents = false |
Model |
Executes an updateOrCreate operation. |
delete() |
string\|int\|Model $id, bool $withoutEvents = false |
bool |
Deletes a model by ID or instance. |
restore() |
string\|int\|Model $id, bool $withoutEvents = false |
bool |
Restores a soft-deleted model. |
forceDelete() |
string\|int\|Model $id, bool $withoutEvents = false |
bool |
Permanently deletes a model. |
Testing & Quality Assurance
License
The MIT License (MIT). Please see LICENSE.md for more information.
All versions of laravel-repository-pattern with dependencies
illuminate/contracts Version ^11.0|^12.0|^13.0
illuminate/database Version ^11.0|^12.0|^13.0
illuminate/support Version ^11.0|^12.0|^13.0
spatie/laravel-package-tools Version ^1.16
spatie/laravel-query-builder Version ^6.0
symfony/finder Version ^6.0|^7.0|^8.0
php Version ^8.2