/** * Execute the job. * * @return void */ public function handle() { $category = Category::findOrFail($this->req->input('category')); $tags = explode(',', $this->req->input('tags')); $tagsCollection = Tag::lists('tag', 'id'); $tagsToAttach = []; foreach ($tags as $key => $value) { $value = trim(ucfirst($value)); $tag = $tagsCollection->search($value); if ($tag) { //echo $tag; $tagsToAttach[] = $tag; } else { $tag = new Tag(); $tag->tag = $value; $tag->save(); $tagsToAttach[] = $tag->id; } } $post = new Post(); $post->title = $this->req->input('title'); $post->user_id = Auth::user()->id; $post->body = $this->req->input('post'); $category->posts()->save($post); $post->tags()->attach($tagsToAttach); return true; }
/** * Bootstrap any application services. * * @return void */ public function boot() { view()->composer('partials.header', function ($view) { $view->with('pages', Page::all()); }); view()->composer('admin.posts.form', function ($view) { $view->with('tags', Tag::lists('name', 'id')); }); }