created() public static method

Register a created model event with the dispatcher.
public static created ( Closure | string $callback, integer $priority ) : void
$callback Closure | string
$priority integer
return void
Example #1
0
 public static function boot()
 {
     parent::creating(function ($m) {
         $m->id = sha1(str_random(128));
     });
     parent::created(function ($m) {
         preg_match_all("/(^|[^a-zA-Z0-9])@([a-zA-Z0-9_])+/i", $m->conteudo, $matches);
         $locais = array();
         if (!empty($matches[0])) {
             foreach ($matches[0] as $match) {
                 array_push($locais, preg_replace('/[^a-zA-Z0-9_]/i', "", $match));
             }
         }
         preg_match_all("/(^|[^a-zA-Z0-9])#([a-zA-Z0-9_])+/i", $m->conteudo, $matches);
         $tags = array();
         if (!empty($matches[0])) {
             foreach ($matches[0] as $match) {
                 array_push($tags, preg_replace('/[^a-zA-Z0-9_]/i', "", $match));
             }
         }
         foreach ($tags as $tag) {
             $t = \App\Tag::firstOrCreate(['tag' => $tag]);
             $m->tags()->save($t);
         }
         foreach ($locais as $local) {
             $l = \App\Local::firstOrCreate(['local' => $local]);
             $m->locais()->save($l);
         }
     });
 }
Example #2
0
 /**
  * Setting handlers on save Entity
  */
 public static function boot()
 {
     parent::boot();
     parent::saving(function (Entity $entity) {
         if (!$entity->exists) {
             $entity->family_id = $entity->set->getNewSet();
         }
     });
     parent::created(function (Entity $entity) {
         $entity->closure->insertNode($entity->id);
     });
 }