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); } }); }