masterbroki/hookable

Laravel Eloquent hooks system.

v10.0.4 2023-05-29 11:18 UTC

This package is auto-updated.

Last update: 2024-10-29 14:12:22 UTC


README

GitHub Tests Action Status stable Downloads

This is a overhaul of the Hookable system from jarektkaczyk/hookable to support laravel 10 and make the system leaner.

Hooks system for the Eloquent ORM (Laravel 10.0).

Hooks are available for the following methods:

  • Model::getAttribute
  • Model::setAttribute
  • Model::save
  • Model::toArray
  • Model::replicate
  • Model::isDirty
  • Model::__isset
  • Model::__unset

and all methods available on the Illuminate\Database\Eloquent\Builder class.

Installation

Clone the repo or pull as composer dependency:

composer require masterbroki/hookable:~10.0

Usage

Use the hookable trait on the model:

class MyModel extends \Illuminate\Database\Eloquent\Model{
    use Sofa\Hookable\Hookable;
}

In order to register a hook, use the static method hook on the model:

MyModel::hook('myMethod', function($myParams){
    return "I want to select: " . implode(', ', $myParams);
});

And when the attribute is called:

$result = MyModel::select(["first", "second"]);

// $result will equal "I want to select: first, second"

Contribution

All contributions are welcome, PRs must be tested.