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

alrocha's avatar

Restore User Soft Deleted

Using the Auth system of laravel, does someone have a clue how to check if the user has been soft deleted in the login or register and restore it?

I did it in a hard way, checking if the users email exist in the table users with the credentials of the log in, and then changing the delete_at to null manually, but is there any semi-auto way to do it?

Thanks in advance

0 likes
10 replies
isaackearl's avatar

You could do a firstOrNew on the user table to grab the related field..

then do something like this

if ($user->trashed()) {
    $user->restore();
}
1 like
alrocha's avatar

@Kryptonit3 I already had a look in the documentation but I can't use those methods in the AuthenticatesAndRegistersUsers.php from laravel.

This is the function

public function postLogin(Request $request) { $this->validate($request, [ 'email' => 'required|email', 'password' => 'required', ]);

    $credentials = $request->only('email', 'password');


    $user = User::where('email','=', ($request->only('email')))->get();
    dd($user);

/* The thing is I don't get anything when the column deleted_at is set with a date

  • if I mark the column as null, I get the user with that email of the request, which I don't really understand, does Laravel *checks even before if the user has been softDeleted?
  • @isaackearl I can't use those methods here, i think is because is a Trait. */

Any other clues?

isaackearl's avatar

@alrocha yes as @bobbybouwmann says you can modify your query to use withTrashed() and it will included deleted models.. then you can use the restore() method to bring it back if it is deleted.

alrocha's avatar

@bobbybouwmann Dankjwell!!! But anyhow I can't keep going further, now I have the user, but I can't access do the trashed method, I mean:

  • $user = User:: withTrashed()->where('email', '=', $request->get('email'))->get();
  • $user->trashed() ERR: Call to undefined method Illuminate\Database\Eloquent\Collection::trashed()

And apparently make sence (for me), I define a variable, pointing to a model User, but I can't accesto the $user->trashed().

I just want to check if the user has been soft deleted, if it have been, sign in and redirect to a view to activate his account, ofcourse I have been set up a middleware to deny the user to do anything else. Does anyone tried this before. Something like facebook, if you deactivate your account you get a flash message to reactivate it. I guess it happens in every social network but just as an example.

Thanks in advance again guys!

alrocha's avatar

Well I just realized with onlyTrashed() I get the user, just if it has been deleted, but afterworkds, I'm still stuck >.<

bobbybouwmann's avatar

Well if you do it like this

$user = User::withTrashed()->where('email', '=', $email)->first(); // This returns the first user with that email address.

// If you want to check if the user is deleted you can do this
if ($user->deleted_at != null) {
    // User is deleted
    // Redirect the user
} 

// Continue the normal flow of your application
alrocha's avatar

I tried it already, but I get this error

Undefined property: Illuminate\Database\Eloquent\Collection::$deleted_at

Please or to participate in this conversation.