I
I
Ivan2021-02-18 15:21:00
Laravel
Ivan, 2021-02-18 15:21:00

How to make a selection of users through the model, except for users with certain emails (like a black list)?

Good afternoon. There is a selection of all users, then they receive emails. And there is a table of emails (like a blacklist), to which nothing needs to be sent. Is it possible to make eloquent without bicycles so that it selects all users, except for those whose emails are on this black list? If so, how.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
K
Konstantin B., 2021-02-18
@Djonson86

WHERE NOT IN method will help you

J
jazzus, 2021-02-18
@jazzus

Adding a relationship to the blacklist table to the User model

public function blackEmail()
{
    return $this->hasOne('App\Models\BlackEmail', 'email', 'email');
}

And we use it everywhere
User::doesntHave('blackEmail')
    ->get();

or add a boolean has_black_email field to the users table
User::where('has_black_email', false)
    ->get();

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question