Guest User

Untitled

a guest
Jun 18th, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.34 KB | None | 0 0
  1. public function update(Request $request, $id)
  2. {
  3. $exploded = explode(',', request('image'));
  4. $decoded = base64_decode($exploded[1]);
  5. if(str_contains($exploded[0], 'jpeg'))
  6. $extension = 'jpg';
  7. else
  8. $extension = 'png';
  9.  
  10. $fileName = str_random().'.'.$extension;
  11. $path = public_path().'/'.$fileName;
  12. file_put_contents($path, $decoded);
  13.  
  14. $post = Post::findOrFail($id);
  15. $post->title = request('title');
  16. $post->description = request('description');
  17. $post->category_id = request('category_id');
  18. $post->user_id = Auth::id();
  19. $post->photo = $fileName;
  20. $post->save();
  21.  
  22. return response()->json([
  23. 'post' => $post,
  24. ], 200);
  25. }
  26.  
  27. <div class="form-group">
  28. <label>Image</label>
  29. <input type="file" @change="imageChanged" class="form-control">
  30. </div>
  31.  
  32. imageChanged(e){
  33. var fileReader = new FileReader()
  34. fileReader.readAsDataURL(e.target.files[0])
  35. fileReader.onload = (e) => {
  36. this.post.photo = e.target.result
  37. }
  38. },
  39.  
  40. editPost(){
  41. axios.patch('/api/posts/' + this.update_post.id, {
  42. title: this.update_post.title,
  43. description: this.update_post.description,
  44. category_id: this.update_post.category_id,
  45. photo: this.update_post.photo
  46. })
  47. .then(response => {
  48. this.showPosts();
  49. })
  50. .catch(function(error){
  51. console.log(error);
  52. });
  53. }
Add Comment
Please, Sign In to add comment