pektiyaz / laravel-repository
Repository implement for Laravel Framework
Requires
- illuminate/support: ^9.0 || ^10.0 || ^11.0 || ^12.0
- pektiyaz/repository-contracts: ^1.0
This package is auto-updated.
Last update: 2025-08-24 15:40:37 UTC
README
A clean, extendable, and event-driven repository pattern implementation for Laravel applications. Easily separate your business logic from the persistence layer using a simple and powerful abstraction.
Built with β€οΈ by Pektiyaz
β¨ Features
- π§© Abstract base repository with out-of-the-box CRUD operations
- π Event-driven architecture (
created
,updated
,deleted
,restored
, etc.) - β»οΈ Soft delete & restore support
- π Powerful filtering with custom
QueryFilterContract
- ποΈ Entity abstraction with transformation helpers (
toArray
,toJson
, etc.) - π¦ Bulk operations (create, update, delete)
- π Pagination and advanced query support via callbacks
π¦ Installation
composer require pektiyaz/laravel-repository
π§° Usage
- Extend the AbstractRepository
use Pektiyaz\LaravelRepository\AbstractRepository; /** * @method PostEntity findById(int|string $id) * @method PostEntity findOneBy(array $conditions) * @method PostEntity[] findAllBy(array $conditions) * @method PostEntity[] findAll() * @method PostEntity create(array $data) * @method PostEntity restore(int $id) * @method PostEntity[] paginate(int $page, int $perPage, array $conditions = []) * @method PostEntity[] findTrashed() * @method PostEntity findTrashedById(int|string $id) * @method PostEntity[] findByCallback(callable $callback) * @method PostEntity[] bulkCreate(array $records) * @method PostEntity[] filter(QueryFilterContract $filter) */ class PostRepository extends AbstractRepository { public function getModel(): string { return \App\Models\Post::class; } public function getEntity(): string { return \App\Entities\PostEntity::class; } public function getEventPrefix(): string { return 'post'; } }
- Create Your Entity
use Pektiyaz\LaravelRepository\AbstractEntity; class PostEntity extends AbstractEntity { protected ?string $title = null; public function getTitle(): ?string { return $this->title; } public function setTitle(?string $title): void { $this->title = $title; } // Add other fields as needed... }
π§ Concepts
π AbstractRepository
The AbstractRepository provides a fully-featured base to handle:
- findById, findOneBy, findAllBy, findAll
- create, update, delete
- restore, forceDelete
- bulkCreate, bulkUpdate, bulkDelete
- paginate, exists, count
- filter, updateByFilter, deleteByFilter, countByFilter
Event dispatching with customizable prefixes
π§± AbstractEntity
The AbstractEntity provides a structured way to transform data between model and entity:
- toArray(), toJson() β Serialize entity
- fromArrayData(array $data) β Hydrate entity
- fromEntity(object $item) β Populate from another entity
- fromJson(string $json) β Load from JSON
π Example Event Dispatching
If your repository uses an event prefix post, the following events will be dispatched automatically:
- post.entity.created
- post.entity.updated
- post.entity.deleted
- post.entity.restored
- post.entity.permanently_deleted
Use Laravelβs event listeners to handle these events for logging, syncing, notifications, etc.
π§ Contracts Required
This package relies on a few contracts to ensure consistency:
- RepositoryContract
- EntityContract
- QueryFilterContract
These can be published or extended as needed for your application structure.