I'm learning laravel and I'm doing a user panel where this has an avatar by default, the user can change their profile image, and I can upload the image and deploy it, the problem is that every time I upload a new image, the old one remains in the directory, as I do so that only the image I upload remains in the directory (without deleting the avatar by default)
public function update (Request $ r) {
if ($r->hasFile('img')) {
$file = $r->file('img');
$filename = time().'-'.$file->getClientOriginalName();
$path = 'avatar';
$file->move($path, $filename);
$user_id = Auth::user()->id;
DB::table('users')->where('id', $user_id)->update(['img' => $filename]);
}
Session::flash('success', 'Profile updated.');
return redirect('profile/'.Auth::user()->name);
}