Skip to content

AbstractRepository ​

v1.1.0

The 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:

php
class UserRepository extends AbstractRepository
{
    public function model(): string
    {
        return User::class;
    }
}

Repository Lifecycle ​

Every database operation follows the same execution pipeline.

ServiceRepositoryModelDatabase

The Repository owns every persistence concern while the Service owns business rules.


Protected Properties ​

$model ​

php
protected Model $model;

Stores the resolved Eloquent Model instance.

The Repository automatically instantiates the Model defined by model().


Required Method ​

model() ​

php
public function model(): string

Every Repository must define which Model it manages.

Example:

php
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:

php
$user = $repository->create([
    'nome' => 'John',
    'email' => 'john@example.com'
]);

Flow:

text
Array
 ↓
Repository
 ↓
Model::create()
 ↓
Database

Returns the created Model.


update() ​

Updates an existing record.

Example:

php
$repository->update($user, [
    'nome' => 'John Updated'
]);

Flow:

  1. Resolve entity.
  2. Fill attributes.
  3. Save changes.

Returns the updated Model.


delete() ​

Deletes a record.

Example:

php
$repository->delete($user);

When SoftDeletes is enabled, the operation becomes a soft delete automatically.


restore() ​

Restores soft-deleted records.

Example:

php
$repository->restore($publicId);

Useful for administrative features.


Find Methods ​

find() ​

Retrieves one entity.

Example:

php
$user = $repository->find($publicId);

Returns null when not found.


findOrFail() ​

Throws an exception when the record does not exist.

Example:

php
$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:

text
/api/users/15

Generated domains use:

text
/api/users/01JXYZABCDEF123456

Supported identifiers:

  • ULID
  • UUID
  • UUID32
  • custom hash identifiers

Collection Methods ​

all() ​

Returns every record.

Example:

php
$users = $repository->all();

Useful for small datasets.

For larger datasets, prefer pagination.


paginate() ​

Returns paginated data.

Example:

php
$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:

php
$repository->with([
    'municipio'
]);

Equivalent to:

php
User::with('municipio');

Benefits:

  • fewer queries
  • predictable responses

where() ​

Add basic conditions.

Example:

php
$repository->where('ativo', true);

whereLike() ​

Perform partial searches.

Example:

php
$repository->whereLike('nome', 'John');

Produces behavior similar to:

sql
WHERE nome LIKE '%John%'

Ideal for search endpoints.


whereIn() ​

Filter multiple values.

Example:

php
$repository->whereIn('perfil', [
    'admin',
    'manager'
]);

orderBy() ​

Sort results.

Example:

php
$repository->orderBy('nome');

Descending:

php
$repository->orderBy('created_at', 'desc');

Chaining Queries ​

One advantage of the generated Repository is fluent chaining.

Example:

php
$repository
    ->with(['municipio'])
    ->where('ativo', true)
    ->orderBy('nome')
    ->paginate();

Readable, reusable and expressive.


Relationship Loading ​

The Repository centralizes eager loading.

Example:

php
$repository->with([
    'municipio',
    'envios'
]);

Instead of repeating relationships throughout Services.


Pagination Flow ​

Pagination follows this execution pipeline.

text
Service
 ↓
Repository
 ↓
Query Builder
 ↓
Paginator
 ↓
Resource Collection

Every generated Controller receives a standardized structure.


SoftDeletes ​

Generated Repositories fully support Laravel's SoftDeletes.

Example Model:

php
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.

Released under the MIT License.