public function testStoreDoesNotDuplicateTags() { $tag = factory(Tag::class)->create(['name' => 'Foo', 'slug' => 'foo']); $input = array_only($tag->getAttributes(), $tag->getFillable()); $this->action('POST', 'Admin\\TagsController@store', $input); $this->assertResponseStatus(201); $this->seeJson(['name' => 'Foo']); $this->assertEquals(1, Tag::count()); }
/** * Execute the command. * * @return \Illuminate\Database\Eloquent\Collection */ public function handle() { foreach ($this->slugs as $slug) { if (!($tag = Tag::where('slug', $slug)->first())) { $tag = Tag::create(['name' => $slug]); } $this->tags[] = $tag; } return new Collection($this->tags); }
/** * Define your route model bindings, pattern filters, etc. * * @param \Illuminate\Routing\Router $router * @return void */ public function boot(Router $router) { parent::boot($router); $router->bind('posts', function ($value, $route) { return Post::whereSlug($value)->firstOrFail(); }); $router->bind('series', function ($value, $route) { return Series::whereSlug($value)->firstOrFail(); }); $router->bind('tags', function ($value, $route) { return Tag::whereSlug($value)->firstOrFail(); }); $router->bind('projects', function ($value, $route) { return Project::whereSlug($value)->firstOrFail(); }); $router->model('users', 'NeonTsunami\\User'); }
/** * POST /admin/tags * Store a new tag in storage. * * @param \Illuminate\Http\Request $request * @return \Illuminate\Http\JsonResponse */ public function store(StoreTagRequest $request) { $tag = Tag::firstOrCreate(['name' => $request->input('name'), 'slug' => str_slug($request->input('name'))]); return response()->json($tag, 201); }
/** * GET /tags/tag * Display the posts of a given tag. * * @param \NeonTsunami\Tag $tag * @return \Illuminate\Http\Response */ public function show(Tag $tag) { $posts = $tag->posts()->published()->latest()->paginate(); return view('tags.show', compact('tag', 'posts'))->withTitle("Posts for {$tag->name}"); }