Neeraj1005
1 month ago
166
2
Laravel

unlink previous image while updating the image

Posted 1 month ago by Neeraj1005

This is my update method. can anyone tell me how can I unlink the image form directory while updating it. I have used the unlink method but it deleted the new one image form directory. can anyone tell me where I have to put unlink method?

    public function update(Request $request, $id)
    {
        $validatedData = $request->validate([
                'title' => 'required|max:255',
                'description' => 'required',
                // 'image' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:2048',
            ]);
        $data = Welcome::findOrFail($id);
        if ($request->hasFile('image')) {
            if ($request->file('image')->isValid()) {

        $image = $request->file('image');
        $originalname = $image->getClientOriginalName();
        $path = $image->move('uploads/media/welcome/', $originalname);
        $data->image = str_replace('\', '/', $path);
            }
        }

        $data->title = $request->title;
        $data->description = $request->description;
        $data->image = $data->image;
        $data->save();

        return redirect()->route('welcome-home.index');
    }

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