在Laravel的Eloquent whereIn方法中的原始声明

I would simply like to run such query:

select * from `users` where SUBSTRING_INDEX(`email`, '@' ,-1) not in ('gmail.com, outlook.com');

Two ways crossed my mind which non of them work:

1

$providers = array('gmail.com', 'outlook.com');

$providers = "'" . implode("', '", $providers) . "'";

User::whereRaw("SUBSTRING_INDEX(`email`, '@' ,-1) not in (?)", $providers);

the above would not work because PDO will escape the "'" characters.

2

User::whereIn(DB::raw("SUBSTRING_INDEX(`email`, '@' ,-1)", $providers);

this one simply does not work. any idea?

Here's a safer way to do it:

$providers = ['gmail.com', 'outlook.com'];

$placeholder = implode(', ', array_fill(0, count($providers), '?'));

User::whereRaw("SUBSTRING_INDEX(`email`, '@' ,-1) not in ($placeholder)", $providers);

This one is working for me. It is very ugly.

// sanitize the array or use an hardcoded one!
$providers = array('gmail.com', 'outlook.com');
$users = User::whereRaw("SUBSTRING_INDEX(`email`, '@' ,-1) NOT IN ('".implode("','", $providers)."')")->get();

I whereRaw for setting up a complex query without having to do multiple calls to the database, by reusing another query that feeds me the results I need. Please note the use of $query->getQuery()->getBindings() for binding the variables of the first query.

    $modelblock = new ModelBlock();
    $blocklink = new BlockLink();
    $traveltour = new TravelTour();

    $page = $this;

    $query = $page->richblock()->getQuery()
    ->join($modelblock->getTable(),
        $blocklink->getTable().'.block_id',
           '=',
        $modelblock->getTable().'.'.$modelblock->getKeyName())
      ->where($modelblock->getTable().'.modelclass','=',get_class($traveltour))
      ->select($blocklink->getTable().'.block_key');

    $ret = $traveltour->newQuery()->select('*')
                      ->whereRaw($traveltour->getKeyName().' in ( '.
                           $query->toSql() . ')'
                           ,$query->getQuery()->getBindings());