Breki

Avoiding dynamic calls

Posted 1 year ago by Breki

Quick question!

Assume I've got a model containg users, which includes usernames, last login time, account creation, e-mails, etc, etc. Let's call the model Users. Now; in many places in the code, I'll be wanting to make checks against this table to see if we're trying to assign an e-mail address to an user when that e-mail address already exists on another user or something like that.

So far, I've been doing this with dynamic calls, things like if (Users::where('email', $this->formResults->email)->count() > 0), (after casting all to lowercase and trimming/validating, of course) but I'm beginning to wonder if Eloquent models should be used in this way or not.

I've seen the alternate usage of:

$userList = new User();
$uCount = $userList->where('email', $this->formResults->email)->count();
if ($uCount > 0) {
    [...]

It's more wordy, for sure, but I'm wondering which is the recommended method?

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