public function run() { //DB::table('post_tag')->delete(); // pivot tag PostTag::create(array('tag_id' => 1, 'post_id' => 1)); }
/** * Tag this post with a particular tag * * @param string $tag */ public function tag($tags, $clearExisting = false) { if (!is_array($tags)) { $tags = array($tags); } if ($clearExisting) { $this->Tags()->removeAll(); } $created = array(); foreach ($tags as $tag) { if (!preg_match('/[a-z0-9_-]/i', $tag)) { continue; } $existing = PostTag::get()->filter(array('Title' => $tag))->first(); if (!$existing) { $existing = PostTag::create(); $existing->Title = $tag; $existing->write(); } $this->Tags()->add($existing, array('Tagged' => date('Y-m-d H:i:s'))); $created[] = $existing; } return $created; }