axolote-source/laravel-logics

Logics library for Laravel projects

Maintainers

Package info

github.com/AxoloteSource/laravel-logics

pkg:composer/axolote-source/laravel-logics

Statistics

Installs: 5

Dependents: 0

Suggesters: 0

Stars: 3

Open Issues: 0

1.0.0 2026-03-25 03:10 UTC

This package is auto-updated.

Last update: 2026-03-25 18:03:49 UTC


README

Latest Version on Packagist Total Downloads

A library for Laravel that provides a structured way to handle business logic using specialized classes (Logics) injected into controllers. This approach helps keep controllers thin and promotes code reuse and testability.

Installation

You can install the package via composer:

composer require axolote-source/laravel-logics

Core Concept: Logics

Logics are classes that encapsulate a specific action on a model (or multiple models). They follow a predefined execution flow:

  1. before(): Validation or pre-processing logic. Returns bool.
  2. action(): The main business logic execution. Returns self.
  3. after(): Post-processing logic (e.g., logging, notifications). Returns bool.

Base Logic Class

The Logic class is the foundation for all other logics. It provides common functionality like error handling and response formatting. Use it when your action doesn't fit into standard CRUD operations.

use AxoloteSource\Logics\Logics\Logic;

class CustomActionLogic extends Logic {
    protected function before(): bool {
        // Validation logic
        return true;
    }

    protected function action(): self {
        // Business logic here
        return $this;
    }

    protected function after(): bool {
        // Post-action logic
        return true;
    }
}

Specialized Logics

The package includes pre-built abstract classes for standard CRUD operations:

1. IndexLogic

Used for listing resources. It includes built-in support for:

  • Pagination: Automatic handling of limit and page.
  • Filtering: Flexible system for applying filters to the query.
  • Search: Basic search functionality on a specified column.

2. StoreLogic

Used for creating new resources. It handles:

  • Data mapping from input to the model.
  • Automatic saving of the model.

3. ShowLogic

Used for retrieving a single resource by its ID or a specific attribute.

4. UpdateLogic

Used for updating an existing resource. Similar to StoreLogic, but works on an existing model instance.

5. DeleteLogic

Used for removing a resource. It can handle both hard and soft deletes.

Flow: Automated Logic Execution

The Flow sub-package allows for a more automated way to handle CRUD operations by mapping routes to models and resources dynamically.

It uses traits like FlowLogic to:

  • Validate if a model is allowed for the current operation.
  • Automatically transform the result using the mapped Eloquent Resource.
  • Handle permissions based on user actions.

Example components of Flow:

  • FlowIndexLogicBase: Base for automated listing.
  • FlowStoreLogicBase: Base for automated creation.
  • FlowShowLogicBase: Base for automated single resource retrieval.
  • FlowUpdateLogicBase: Base for automated updating.
  • FlowDeleteLogicBase: Base for automated deletion.

Benefits

  • Structured Code: Clearly defined execution steps (before, action, after).
  • Consistency: Standardized way of handling CRUD across the application.
  • Reusability: Core logic is decoupled from controllers.
  • Extensibility: Easily extend base classes to add custom behavior.

Testing

To run the package tests, install dependencies and execute PHPUnit:

composer install
vendor/bin/phpunit

License

MIT