Skip to content

Instantly share code, notes, and snippets.

@jhaoda
Last active February 3, 2016 19:32
Show Gist options
  • Save jhaoda/2d6263449e7c7ec84fea to your computer and use it in GitHub Desktop.
Save jhaoda/2d6263449e7c7ec84fea to your computer and use it in GitHub Desktop.
<?php
class Model extends \Eloquent
{
protected $observables = ['readed'];
protected static function boot()
{
parent::boot();
$hooks = ['before' => 'ing', 'after' => 'ed'];
$radicals = ['sav', 'creat', 'updat', 'delet'];
foreach ($radicals as $rad) {
foreach ($hooks as $hook => $event) {
$method = $hook . ucfirst($rad) . 'e';
$eventMethod = $rad . $event;
static::$eventMethod(function($model) use ($method) {
return $model->$method($model);
});
}
}
static::readed(function ($model) {
return $model->{'afterRead'}($model);
});
}
public function setRawAttributes(array $attributes, $sync = false)
{
parent::setRawAttributes($attributes, $sync);
$this->fireModelEvent('readed');
}
public static function readed($callback)
{
static::registerModelEvent('readed', $callback);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment