/** * Attach Tags * */ protected function attachTags($tags, $object, $separator = ',', $replace = true) { \Eloquent::unguard(); $tagArr = []; if (is_array($tags)) { $matches[1] = $tags; } elseif (!empty($separator)) { $matches[1] = explode($separator, $tags); } else { preg_match_all("/^(.*)\$/m", trim($tags), $matches); } if (!empty($matches[1]) && is_array($matches[1])) { foreach ($matches[1] as $tag) { $tag = trim($tag); if (empty($tag)) { continue; } $tagDb = \Veer\Models\Tag::firstOrNew(['name' => $tag]); if (!$tagDb->exists) { $tagDb->name = $tag; $tagDb->save(); } $tagArr[] = $tagDb->id; } } $this->attachElements($tagArr, $object, 'tags', null, ",", ":", $replace); }
public function add($tag) { $this->entity = \Veer\Models\Tag::firstOrNew(['name' => $tag]); $this->entity->save(); }