简体   繁体   English

Laravel whereNotIn 未按预期查询

[英]Laravel whereNotIn not querying as expected

I have a scoped query in my user class like so ( User.php ):我的用户类中有一个范围查询,如下所示( 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.我从一个集合中获取我的$userIds ,该集合返回一个id数组。 Here is my collection method ( ImportUsers.php ):这是我的收集方法( 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 ).这里是我调用getUsersToDelete方法以便让用户删除 ( 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.如您所见,我正在转储 SQL,以便我可以看到查询,因为它不起作用。 When I dump the query, I get this:当我转储查询时,我得到这个:

原始查询

However, when I dd($userIds) the line before the toSql() , like so ( User.php ):但是,当我dd($userIds) toSql()toSql()之前的toSql() ,像这样( 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 。这是文档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您还可以在scotch.io 上找到详细信息

I hope this may help you.我希望这可以帮助你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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