https://github.com/sarfraznawaz2005/eventable
Laravel package to easily add event listening capabilities to any model on Create/Update/Delete operations.
https://github.com/sarfraznawaz2005/eventable
eloquent events laravel laravel-5-package model php
Last synced: 20 days ago
JSON representation
Laravel package to easily add event listening capabilities to any model on Create/Update/Delete operations.
- Host: GitHub
- URL: https://github.com/sarfraznawaz2005/eventable
- Owner: sarfraznawaz2005
- Created: 2017-09-16T21:37:41.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2017-09-16T21:59:07.000Z (over 7 years ago)
- Last Synced: 2025-03-21T07:41:36.351Z (about 1 month ago)
- Topics: eloquent, events, laravel, laravel-5-package, model, php
- Language: PHP
- Size: 1.95 KB
- Stars: 3
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Laravel Eventable
[](http://laravel.com)
[](http://laravel.com)
[](http://laravel.com)
[](http://laravel.com)
[](https://packagist.org/packages/sarfraznawaz2005/eventable)## 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](http://opensource.org/licenses/MIT).
This means you can do almost anything with it, as long as the copyright notice and the accompanying license file is left intact.