library laravel-eloquent-observer
Dispatch Eloquentâs models events to the modelâs instance, and / or a specific class
ronan-gloo/laravel-eloquent-observer
Dispatch Eloquentâs models events to the modelâs instance, and / or a specific class
- Saturday, September 6, 2014
- by ronan-gloo
- Repository
- 2 Watchers
- 5 Stars
- 2 Installations
- PHP
- 0 Dependents
- 0 Suggesters
- 3 Forks
- 0 Open issues
- 1 Versions
- 0 % Grown
Laravel Observer
Dispatch Eloquentâs models events to the modelâs instance, and / or a specific class, (*1)
Installation
php artisan bundle:install observer, (*2)
Events
-
saving: before save
-
saved: after save
-
updated: after update
-
created: after creation
-
deleting: defore delete
-
deleted: after delete
Usage
1. Whithin the model
class Post extends Eloquent {
/**
* This method will be run after an update or a creation.
*/
public function event_saved()
{
Log::info(get_class($this).' with title "'.$this->title.'" saved');
}
}
2. With an Observer
// The Model
class Post extends Eloquent {
public static $observe = array(
// Single observer for a single event
'saving' => 'Observe_Slug',
// Multiple observers for a single event
'created' => array('Observe_Log', 'Observe_Mail'),
// Observer with parameters
'updated' => array('Observe_History' => array('log' => true))
);
}
// Observer
class Observer_Slug extends Observer\Observe {
// Modelfy object before to save it
public function saving($model)
{
model->slug = Str::slug($model->title);
}
// event with parameters: parameters from model are instance properties here
public function updated($model)
{
if ($this->log == true)
{
Do something...
}
}
}
dev-master
9999999-dev
Dispatch Eloquentâs models events to the modelâs instance, and / or a specific class
Sources
Download
by
ronan-gloo