'Correct image path to delete from storage laravel 8

I try to delete the old profilepicture in storage file but it is not working. below is my screenshot of my files

enter image description here

and this is my code in update profile controller.

public function updateProfile(Request $request, User $user)
{
    if ($request->hasFile('profilepicture')) {

        Storage::delete('/storage/profilepicture' . $user->profilepicture);

        $filenameWithExt = $request->file('profilepicture')->getClientOriginalName();
        $filename = pathinfo($filenameWithExt, PATHINFO_FILENAME);
        $extension = $request->file('profilepicture')->getClientOriginalExtension();
        $fileNameToStore =  time() . '.' . $extension;
        $path = $request->file('profilepicture')->storeAs('public/profilepicture', $fileNameToStore);

        $user->update([
            'profilepicture' => $fileNameToStore
        ]);
        
    }

    $user->update([
        'name' => $request->name,
        'email' => $request->email,
        'birth_date' => $request->birth_date,
        'section' => $request->section,
        'unit' => $request->unit,
        'phone' => $request->phone,
    ]);

    return back()->withSuccess('You have successfully update User.');
}

i already tried

Storage::delete('/public/storage/profilepicture/' . $user->profilepicture);
Storage::delete('/storage/profilepicture/' . $user->profilepicture);
Storage::delete('profilepicture/' . $user->profilepicture);

but none of them is working. Or there is any other correct way to do this?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source