sarfraznawaz2005/eventable

Laravel 5 package to easily add event listening capabilities to any model on Create/Update/Delete operations.

1.0.0 2017-09-16 21:33 UTC

This package is auto-updated.

Last update: 2024-11-12 05:02:37 UTC


README

laravel 5.1 laravel 5.2 laravel 5.3 laravel 5.4 downloads

Introduction

Simple Laravel 5 package to easily add event listening capabilities to any model on Create/Update/Delete operations.

Requirements

  • PHP >= 5.6
  • Laravel 5

Installation

Install via composer

composer require sarfraznawaz2005/eventable

That's it!

Usage

Suppose you have a model called Task and you want to be able to do something when it's created/updated or deleted. To do that, just use the Eventable trait like so:

...
use Sarfraznawaz2005\Eventable\Eventable;

class Task extends Model
{
    use Eventable;
    
    ...
}

Now somewhere in your app, you can listen to events and do whatever you want:

Event::listen('task.created', function ($task) {
    // do something when task is created. In this case, just log it.
    Log::info('Task with id ' . $task->id . ' was created.');
});

Event::listen('task.updated', function ($task) {
    // do something when task is updated. In this case, just log it.
    Log::info('Task with id ' . $task->id . ' was updated.');
});

Event::listen('task.deleted', function ($task) {
    // do something when task is deleted. In this case, just log it.
    Log::info('Task with id ' . $task->id . ' was deleted.');
});

Note: Make sure your put event listening logic BEFORE saving/updating/deletng model logic.

License

This code is published under the MIT License. This means you can do almost anything with it, as long as the copyright notice and the accompanying license file is left intact.