$kirby->user($id)->exists() throws Call to a member function exists() on null if user does not exist

I’m having problems with $kirby->user($id)->exists(). I’m trying to use it as mentioned here ($user->exists() | Kirby CMS), to verify, whether a user with a given id exists. I was however getting an error with the exact code provided in the example and did some further digging. It says here ($kirby->user() | Kirby CMS) that $kirby->user($id) returns a user object or null (as far as I could understand the codebase, this is the case if the user is not found?). So I was thinking null can’t then call exists(), right? Am I not getting something here or is the method shown in the example of the kirby page flawed from an update?
Instead I’m now using $kirby->user($id) === null, which seems to correctly output whether a user exists, but I am unsure, if I am handling this correctly.
Thanks in advance for any inputs :slight_smile:

Yes, the method requires a user object to already exist, so the example doesn’t make sense. Sorry for that.

Use

if ($user = $kirby->user($id)) {
  // do stuff
}

instead.

Or the other way round, as you already found … But I didn’t read to the end before I answered :see_no_evil:

1 Like

Ok perfect, thank you for answering so fast :slight_smile: