Save user image url in database

Asked by At

I save the image in the public folder but i need the image url saved in the database instead of the file name.

https://devdojo.com/episode/laravel-user-image

public function update_avatar(Request $request){

      if ($request->hasFile('avatar')){
        $avatar = $request ->file('avatar');



        $fileName = time().'.'. $avatar->getClientOriginalExtension();

        Image::make($avatar)->resize(300, 300)->save( public_path('/uploads/avatars/'. $fileName));

        $user = Auth::user();
        $user->avatar = $fileName;
        $user->save();

      }
      return view('profile', array('user' => Auth::user()));
    }

3 Answers

-2
nyu.exe On

You should convert img to base64 to store it as a string in DB.

I never recommend this, but anyway:

$data = base64_encode($img); 
2
livreson ltc On

It is highly not recommend to use that approach, instead save the name in database, and use the file path with the name on the database to display the image.

For example:

<img class="border-radius-3" src="{{ asset('uploads/avatars/'.$user->image)}}" alt="">

Just tell us what you're trying to accomplish.

1
Yegor Keller On

If you need save file URL in DB you can use something like this

 $user->avatar = '/uploads/avatars/'.$fileName);