Neeraj1005
1 month ago
171
1
Laravel

photo update, removing previous/old photo

Posted 1 month ago by Neeraj1005

In my laravel project for uploading the file I'm using the spatie media library package. But using this I'm facing the problem with updating the image. Basically for updating the image, when I upload the new image I want to remove old or previous image if post having the image. But in case it store as new image. can anyone knows how to do this update image and remove previous image using spatie media lib pkg? This is my update method

  public function update(PostStoreRequest $request, Post $post)
    {
        $input = $request->validated();

        $post->update($input);

        $post->tags()->sync(request('tags'));

        if($request->hasFile('featuredimage')){
            $post->addMedia($request->featuredimage)->toMediaCollection('posts');
        }

        return redirect(route('posts.index'))->withInfo('Posts updated succesfully');
    }

Please sign in or create an account to participate in this conversation.