简体   繁体   中英

Laravel whereNotIn not querying as expected

I have a scoped query in my user class like so ( User.php ):

public function scopeDeleteUsersWithId($query, $userIds)
{
    return $query->whereNotIn('id', $userIds)->toSql();
}

I get my $userIds from a collection which returns an array of id 's. Here is my collection method ( ImportUsers.php ):

private function getUsersToDelete()
{
    return $this->employeeList->pluck('id')->unique()->flatten()->toArray();
}

And here is where I call the getUsersToDelete method in order to get the users to delete ( ImportUsers.php ).

private function deleteTerminatedEmployees()
{
    $usersToDelete = $this->getUsersToDelete();

    dd(User::deleteUsersWithId($usersToDelete));
}

As you can see, I am dumping the SQL so that I can see the query since it is not working. When I dump the query, I get this:

原始查询

However, when I dd($userIds) the line before the toSql() , like so ( User.php ):

public function scopeDeleteUsersWithId($query, $userIds)
{
    dd($userIds);
    // return $query->whereNotIn('id', $userIds)->toSql();
}

I get the array that I am looking for:

在此处输入图片说明

I have spent a lot of time trying to figure this out and cannot. Any ideas would be greatly appreciated.

You may use DB::listen for this.Here is the documentation DB::listen

DB::listen(function ($query) {
            var_dump($query->sql); 
            var_dump($query->bindings); 
            var_dump($query->time); 
});

You can also find details at scotch.io

I hope this may help you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM