Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public function update(Request $request, $id)
- {
- $exploded = explode(',', request('image'));
- $decoded = base64_decode($exploded[1]);
- if(str_contains($exploded[0], 'jpeg'))
- $extension = 'jpg';
- else
- $extension = 'png';
- $fileName = str_random().'.'.$extension;
- $path = public_path().'/'.$fileName;
- file_put_contents($path, $decoded);
- $post = Post::findOrFail($id);
- $post->title = request('title');
- $post->description = request('description');
- $post->category_id = request('category_id');
- $post->user_id = Auth::id();
- $post->photo = $fileName;
- $post->save();
- return response()->json([
- 'post' => $post,
- ], 200);
- }
- <div class="form-group">
- <label>Image</label>
- <input type="file" @change="imageChanged" class="form-control">
- </div>
- imageChanged(e){
- var fileReader = new FileReader()
- fileReader.readAsDataURL(e.target.files[0])
- fileReader.onload = (e) => {
- this.post.photo = e.target.result
- }
- },
- editPost(){
- axios.patch('/api/posts/' + this.update_post.id, {
- title: this.update_post.title,
- description: this.update_post.description,
- category_id: this.update_post.category_id,
- photo: this.update_post.photo
- })
- .then(response => {
- this.showPosts();
- })
- .catch(function(error){
- console.log(error);
- });
- }
Add Comment
Please, Sign In to add comment