Closed yuriang85 closed 4 years ago
Hi @yuriang85,
The findBy
method will return null
when no results are returned from your LDAP directory.
You must use:
$user = User::findBy('mail', 'user@local.com');
if ($user) {
$user->delete();
}
Conversely, if you want to avoid checking for null
, you can instead use the findByOrFail
method that will throw an exception instead:
try {
$user = User::findByOrFail('mail', 'user@local.com');
$user->delete();
} catch (LdapRecord\Models\ModelNotFoundException $e) {
// User not found.
}
Docs:
https://ldaprecord.com/docs/searching/#executing-searches
I would check your directory to ensure there is indeed a user with the given mail
attribute you are looking for.
I would also ensure that your User
model does not have any global scopes that may be affecting the lookup of this user in your directory.
OK thank you very much. resolved!
Glad to help @yuriang85! Feel free to create another issue if you have any further questions :smile:
$user = User::findBy('mail', 'user@local.com');
if (!$user->delete()) {
return false;
} else
return true;
returns this: Call to a member function delete () on null.