AbstractRepository
v1.1.0The AbstractRepository is the persistence layer of Laravel Domain Generator.
Instead of scattering database queries across Services or Controllers, every generated domain centralizes persistence inside a Repository.
Every Repository generated by php artisan make:domain extends this class automatically.
Overview
Generated Repositories provide a reusable and predictable API for database operations.
Built-in features include:
- CRUD operations
- Pagination
- Public identifier lookup
- Relationship eager loading
- Query Builder delegation
- SoftDeletes compatibility
- Dynamic filtering
- Ordering
- Reusable query methods
Minimal example:
class UserRepository extends AbstractRepository
{
public function model(): string
{
return User::class;
}
}Repository Lifecycle
Every database operation follows the same execution pipeline.
The Repository owns every persistence concern while the Service owns business rules.
Protected Properties
$model
protected Model $model;Stores the resolved Eloquent Model instance.
The Repository automatically instantiates the Model defined by model().
Required Method
model()
public function model(): stringEvery Repository must define which Model it manages.
Example:
public function model(): string
{
return User::class;
}This is the only method that usually needs to be implemented manually.
CRUD Methods
create()
Creates a new record.
Example:
$user = $repository->create([
'nome' => 'John',
'email' => 'john@example.com'
]);Flow:
Array
↓
Repository
↓
Model::create()
↓
DatabaseReturns the created Model.
update()
Updates an existing record.
Example:
$repository->update($user, [
'nome' => 'John Updated'
]);Flow:
- Resolve entity.
- Fill attributes.
- Save changes.
Returns the updated Model.
delete()
Deletes a record.
Example:
$repository->delete($user);When SoftDeletes is enabled, the operation becomes a soft delete automatically.
restore()
Restores soft-deleted records.
Example:
$repository->restore($publicId);Useful for administrative features.
Find Methods
find()
Retrieves one entity.
Example:
$user = $repository->find($publicId);Returns null when not found.
findOrFail()
Throws an exception when the record does not exist.
Example:
$user = $repository->findOrFail($publicId);This keeps Laravel's familiar behavior while supporting public identifiers.
findByPublicId()
One of the most important generated methods.
Instead of exposing internal IDs:
/api/users/15Generated domains use:
/api/users/01JXYZABCDEF123456Supported identifiers:
- ULID
- UUID
- UUID32
- custom hash identifiers
Collection Methods
all()
Returns every record.
Example:
$users = $repository->all();Useful for small datasets.
For larger datasets, prefer pagination.
paginate()
Returns paginated data.
Example:
$repository->paginate(15);Response contains:
- data
- links
- meta
The Controller receives pagination already prepared.
Query Builder Methods
The Repository exposes fluent query building while hiding persistence details.
with()
Eager-load relationships.
Example:
$repository->with([
'municipio'
]);Equivalent to:
User::with('municipio');Benefits:
- fewer queries
- predictable responses
where()
Add basic conditions.
Example:
$repository->where('ativo', true);whereLike()
Perform partial searches.
Example:
$repository->whereLike('nome', 'John');Produces behavior similar to:
WHERE nome LIKE '%John%'Ideal for search endpoints.
whereIn()
Filter multiple values.
Example:
$repository->whereIn('perfil', [
'admin',
'manager'
]);orderBy()
Sort results.
Example:
$repository->orderBy('nome');Descending:
$repository->orderBy('created_at', 'desc');Chaining Queries
One advantage of the generated Repository is fluent chaining.
Example:
$repository
->with(['municipio'])
->where('ativo', true)
->orderBy('nome')
->paginate();Readable, reusable and expressive.
Relationship Loading
The Repository centralizes eager loading.
Example:
$repository->with([
'municipio',
'envios'
]);Instead of repeating relationships throughout Services.
Pagination Flow
Pagination follows this execution pipeline.
Service
↓
Repository
↓
Query Builder
↓
Paginator
↓
Resource CollectionEvery generated Controller receives a standardized structure.
SoftDeletes
Generated Repositories fully support Laravel's SoftDeletes.
Example Model:
use SoftDeletes;Available operations include:
- delete
- restore
- trashed lookups
- force delete (when implemented)
Performance Considerations
Prefer:
with()paginate()- selective filters
Avoid:
- loading unnecessary relationships
- calling
all()on very large tables
Best Practices
Keep Repositories focused on persistence.
Recommended:
- build queries
- load relationships
- paginate
- retrieve entities
Avoid placing business rules inside Repositories.
Those belong inside the Service layer.
Generated Repositories provide a consistent persistence API while keeping business logic completely separated from database concerns.