pektiyaz/laravel-repository

Repository implement for Laravel Framework

v1.0.1 2025-06-24 15:23 UTC

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

  1. 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';
    }
}
  1. 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:

  1. RepositoryContract
  2. EntityContract
  3. QueryFilterContract

These can be published or extended as needed for your application structure.