protected function loadTags() { $query = Tag::with('posts'); if (!$this->property('emptyTag')) { $query->has('posts', '>=', '1'); } if ($take = intVal($this->property('results'))) { $query->take($take); } $query->listTags($this->property('sortOrder')); return $query->get(); }
/** * get tag id if exists or create it if not exists * * @return array */ public function getSaveValue($value) { $tags = explode(",", implode(",", $value)); $ids = []; foreach ($tags as $name) { if (empty($name)) { continue; } $created = Tag::firstOrCreate(['name' => $name]); $ids[] = $created->id; } return $ids; }
<?php use Rahman\BlogTags\Models\Tag; use RainLab\Blog\Models\Post; /** * get available tags and assigned tags by post id */ Route::get('api/blog/post/{postId?}', ['as' => 'api.get.taglist', function ($postId = 0) { $availableTags = Tag::all()->lists('name'); $assignedTags = Tag::whereHas('posts', function ($q) use($postId) { $q->where('id', $postId); })->lists('name'); $response = ['assignedTags' => $assignedTags, 'availableTags' => $availableTags]; return Response::json($response); }]);
protected function loadTag() { return Tag::where('slug', $this->property('slug'))->first(); }