Guest User

Untitled

a guest
May 25th, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.27 KB | None | 0 0
  1. /**
  2. * Update the specified resource in storage.
  3. *
  4. * @param IlluminateHttpRequest $request
  5. * @param int $id
  6. * @return IlluminateHttpResponse
  7. */
  8. public function update(Request $request, $id)
  9. {
  10. $validatedData = $request->validate([
  11. 'title' => 'required',
  12. 'excerpt' => 'required',
  13. ]);
  14.  
  15. $article = Article::find($id);
  16.  
  17. $article->title = $request->get('title');
  18. $article->author = $request->get('author');
  19. $article->category = $request->get('category');
  20. $article->excerpt = $request->get('excerpt');
  21. $article->content = $request->get('content');
  22. $article->featuredImage = $request->get('featuredImage');
  23. $article->featuredVideo = $request->get('featuredVideo');
  24. $article->readingTime = $request->get('readingTime');
  25. $article->published = $request->get('published');
  26.  
  27. $article->save();
  28.  
  29. /**
  30. * Once the article has been saved, we deal with the tag logic.
  31. * Grab the tag or tags from the field, sync them with the article
  32. */
  33. $tags = $request->get('tags');
  34. $comma = ',';
  35.  
  36. if (!empty($tags)) {
  37. if (strpos($tags, $comma) !== false) {
  38. $tagList = explode(",", $tags);
  39.  
  40. // Loop through the tag array that we just created
  41. foreach ($tagList as $tags) {
  42.  
  43. // Get any existing tags
  44. $tag = Tag::where('name', '=', $tags)->first();
  45.  
  46. // If the tag exists, sync it, otherwise create it
  47. if ($tag != null) {
  48. $article->tags()->sync($tag->id);
  49. } else {
  50. $tag = new Tag();
  51.  
  52. $tag->name = $tags;
  53. $tag->slug = str_slug($tags);
  54.  
  55. $tag->save();
  56.  
  57. $article->tags()->sync($tag->id);
  58. }
  59. }
  60. } else {
  61. // Only one tag
  62. $tag = Tag::where('name', '=', $tags)->first();
  63.  
  64. if ($tag != null) {
  65. $article->tags()->sync($tag->id);
  66. } else {
  67. $tag = new Tag();
  68.  
  69. $tag->name = $tags;
  70. $tag->slug = str_slug($tags);
  71.  
  72. $tag->save();
  73.  
  74. $article->tags()->sync($tag->id);
  75. }
  76. }
  77. }
  78.  
  79. return back();
  80. return redirect()->back();
  81. }
Add Comment
Please, Sign In to add comment