【问题标题】:Laravel 5.4 Query Builder WHERE NOT IN with multiple WHERELaravel 5.4 查询生成器 WHERE NOT IN 与多个 WHERE
【发布时间】:2017-09-07 03:18:57
【问题描述】:
select users.id, users.nickname, users.comment from users 
where users.id <> '.$userId.' and users.id NOT IN
(SELECT friends.friend_id from friends WHERE friends.user_id = '.$userId.')

我已经关注 Laravel Query Builder WHERE NOT IN 并像这样转换了我的查询

 $userId = 1;
 $friend = DB::table('users')
                ->select('users.id, users.nickname, users.comment')
                ->where('users.id', '!=', $userId)
                ->whereNotIn('users.id', function($query){
                    $query->select('friends.friend_id')
                          ->from('friends')
                          ->where('friends.user_id', '=', $userId);
                })->paginate($limit);

未定义变量:userId : ->where('friends.user_id', '=', $userId);

【问题讨论】:

    标签: laravel-5.4


    【解决方案1】:

    您必须使用use 将变量传递给匿名函数。

    -&gt;whereNotIn('users.id', function($query) use ($userId) {

    【讨论】:

      猜你喜欢
      • 2016-04-10
      • 2014-03-03
      • 1970-01-01
      • 2018-01-03
      • 2018-12-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多