Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Update the specified resource in storage.
- *
- * @param IlluminateHttpRequest $request
- * @param int $id
- * @return IlluminateHttpResponse
- */
- public function update(Request $request, $id)
- {
- $validatedData = $request->validate([
- 'title' => 'required',
- 'excerpt' => 'required',
- ]);
- $article = Article::find($id);
- $article->title = $request->get('title');
- $article->author = $request->get('author');
- $article->category = $request->get('category');
- $article->excerpt = $request->get('excerpt');
- $article->content = $request->get('content');
- $article->featuredImage = $request->get('featuredImage');
- $article->featuredVideo = $request->get('featuredVideo');
- $article->readingTime = $request->get('readingTime');
- $article->published = $request->get('published');
- $article->save();
- /**
- * Once the article has been saved, we deal with the tag logic.
- * Grab the tag or tags from the field, sync them with the article
- */
- $tags = $request->get('tags');
- $comma = ',';
- if (!empty($tags)) {
- if (strpos($tags, $comma) !== false) {
- $tagList = explode(",", $tags);
- // Loop through the tag array that we just created
- foreach ($tagList as $tags) {
- // Get any existing tags
- $tag = Tag::where('name', '=', $tags)->first();
- // If the tag exists, sync it, otherwise create it
- if ($tag != null) {
- $article->tags()->sync($tag->id);
- } else {
- $tag = new Tag();
- $tag->name = $tags;
- $tag->slug = str_slug($tags);
- $tag->save();
- $article->tags()->sync($tag->id);
- }
- }
- } else {
- // Only one tag
- $tag = Tag::where('name', '=', $tags)->first();
- if ($tag != null) {
- $article->tags()->sync($tag->id);
- } else {
- $tag = new Tag();
- $tag->name = $tags;
- $tag->slug = str_slug($tags);
- $tag->save();
- $article->tags()->sync($tag->id);
- }
- }
- }
- return back();
- return redirect()->back();
- }
Add Comment
Please, Sign In to add comment