I have a function that takes submitted form data which is an image, then validates it and it deletes an old image if one already exists. It currently does that and stores the image on in the storage folder.
I am now trying to save the URL of the image to the database. I have seen examples where they do not save the path to the database but would it be best to do so?
Here is my function
public function postAvatarUpload(Request $request)
{
$this->validate($request, [
'image' => 'required|image|max:3000|mimes:jpeg,jpg,bmp,png',
]);
$user = Auth::user();
$usersname = $user->username;
$file = $request->file('image');
$filename = $usersname . '.jpg';
if (Storage::disk('local')->has($filename)) {
Storage::delete($filename);
}
Storage::disk('local')->put($filename, File::get($file));
$avatarPath = Storage::url($filename);
Auth::user()->update([
'image' => $avatarPath,
]);
return redirect()->route('profile.index',
['username' => Auth::user()->username]);
}