11

I'm trying to build a function where a user can delete their own account while they are logged in. I'm struggling to find any example or logic/best practices.

The controller looks like this:

public function postDestroy() {

        $user = User::find(Auth::user()->id);
        $user = DB::delete('delete from users')->user(id);
        return Redirect::route('site-home')->with('global', 'Your account has been deleted!');

}

I'm trying to grab the current Auth (logged in) user and use their id to delete them from the database. Then send them to the home page with a message.

Also, do I need to make sure the session is properly closed during this process, such as Auth::logout(); ?

I'm pretty new to Laravel, so any help would be appreciated.

2 Answers 2

19

Not sure how your routing looks like, but this should do the job.

    $user = \User::find(Auth::user()->id);

    Auth::logout();

    if ($user->delete()) {

         return Redirect::route('site-home')->with('global', 'Your account has been deleted!');
    }

You should logout user before delete.

1
  • 1
    You legend! Works perfectly :) Commented Aug 7, 2014 at 20:45
0

You merely gotta do like this:

$user=auth()->user();      
$user->delete();
1
  • there is no method called delete in that object Commented Aug 15 at 3:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.