首页 > 解决方案 > 查询状态以获取未来时间的记录,查询也获取过去的记录

问题描述

我有一个查询,我试图从未来获取所有获取记录。它还获得了过去的记录。

$upcomingSupportGroups = DB::table('participants')
              ->join('threads', 'participants.thread_id', '=', 'threads.id')
              ->join('support_groups', 'threads.counselling_application_id', '=', 'support_groups.id')
              ->where('participants.user_id', auth()->user()->id)
              ->orWhere('support_groups.counsellor_id', auth()->user()->id)
              ->where('support_groups.event_time', '>=', Carbon\Carbon::now())
              ->groupBy('support_groups.id')
              ->get();

我哪里错了

标签: phplaravel

解决方案


因为您有一个,所以orWhere('support_groups.counsellor_id', auth()->user()->id)您可以获得顾问是身份验证用户的所有行。您要将查询更改为:

$upcomingSupportGroups = DB::table('participants')
          ->join('threads', 'participants.thread_id', '=', 'threads.id')
          ->join('support_groups', 'threads.counselling_application_id', '=', 'support_groups.id')
          ->where(function($query){
               $query->where('participants.user_id', auth()->user()->id);
               $query->orWhere('support_groups.counsellor_id', auth()->user()->id)
          })
          ->where('support_groups.event_time', '>=', Carbon\Carbon::now())
          ->groupBy('support_groups.id')
          ->get();

推荐阅读