BrownieCoffee
4 months ago
344
3
Laravel

Delete user's avatar folder after his deletion.

Posted 4 months ago by BrownieCoffee

Hello, there,

I trying to delete user's folder after his deletion.

Actually, in my storage folder have storage/app/public/avatars/[user-folder].

I tryed my code but it fails


      //si l'utilisateur existe
        if ($user) {
            //je détache le role de l'utilisateur
            $user->roles()->detach();
            //je supprimer l'utilisateur
            $user->delete();

            // si l'utilisateur est supprimé
            if ($user->delete()) {

//CODE HERE
                if (Storage::exists($storagePath)) {
                    //je le créé
                    Storage::deleteDirectory($storagePath, 'public');
                }

//END OF CODE
                //je notifie l'utilisateur que son compte à bien été supprimé.
                $user->notify(new SendEmailToUserReferingToDeletingProfile($user));
            }

            return redirect()->route('home')->with('status', 'Votre compte a bien été supprimé.');
        }

Do you have a better solution to suggest me ?

Thank you in avance. GB

Please sign in or create an account to participate in this conversation.