Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Syrine123's avatar

How update picture with cloudinary

Actually I'm on reactjs project with laravel api and I want to update picture with cloudinary. It's done for adding and displaying but I still haven't idea on how to update uploaded picture This the update function:

  public function updateUser(Request $request, $user_id){
    $data = $request->validate([
        'user_name' => '',
        'user_pre_address' => '',
        'user_per_address' => '',
        'password' => '',
        'user_currlang' => '',
        'user_nid' => '',
        'user_tel' => '',
        'user_image' => '',
     
    ]);
    $input = $request->all();
    $user = User::findOrFail($user_id);
    $user->update($input);
    if($user->save()){

      return response()->json([
        'message' => 'Successfully updated user!',
        'status' => 200,
    ]);}
    return $user;
}

This is How I added the picture:

    $user->user_image = Cloudinary::upload(
                    $request->file('user_image')->getRealPath(),
                    [
                        'folder' => 'Testing'
                    ]
                )->getSecurePath();

Thanks in advance for your help Still haven't solution yet...

0 likes
2 replies
drehimself's avatar

You can check if the user has a profile photo and if they do, delete it first before uploading the new image.

Someone has a similar question on the issues: https://github.com/cloudinary-labs/cloudinary-laravel/issues/67

You can do something like this (rough code):

Route::post('/upload', function (Request $request) {
    if (auth()->user()->profile_photo_id) {
        // Cloudinary::destroy('testing/jt5dlp6y4st3dc9u0nqk');
        Cloudinary::destroy(auth()->user()->profile_photo_id);
        auth()->user()->profile_photo_id = null;
        auth()->user()->save();
    }

    $cloudinaryUpload = Cloudinary::upload(
        $request->file('file_upload')->getRealPath(),
        [ 'folder' => 'testing' ]
    );

    auth()->user()->profile_photo_id = $cloudinaryUpload->getPublicId();
    auth()->user()->profile_photo = $cloudinaryUpload->getSecurePath();
    auth()->user()->save();

    return back();
});

So you would store profile_photo_id so you can easily delete it if needed and you would also store profile_photo for the full path to the image.

1 like

Please or to participate in this conversation.