我想只返回每个线程一次,但是我不断收到错误,说我必须在groupBy子句中有其他项目。这使得一些线程显示不止一次(第一次发布一次,如果我已经投票,则再次显示)。我怎样才能将它限制为1次,因为它更喜欢填充threads_users.user_id的时间。
$threads = Thread::leftjoin('users', 'users.id', '=', 'threads.user_id')
->leftjoin('users as poster','poster.id', '=', 'threads.user_id')
->leftjoin('lewps_threads', 'lewps_threads.thread_id','=','threads.id')
->leftjoin('threads_users', 'threads_users.thread_id','=','threads.id')
->where('threads.id', $id)
->wherein('threads_users.user_id', array(null, $users->id))
->groupBy(array('threads.id', 'users.username','threads_users.boosts'))
->get(array('threads.*','users.username','threads_users.boosts'));
答案 0 :(得分:0)
要了解正在进行的操作以及在幕后执行的查询,您可以在执行查询后将其放入:
dd(DB::getQueryLog());
要仅返回1个原始数据,您可以将其添加到您的代码中:
->take(1)->get();
所以这就是这样的:
$threads = Thread::leftjoin('users', 'users.id', '=', 'threads.user_id')
->leftjoin('users as poster','poster.id', '=', 'threads.user_id')
->leftjoin('lewps_threads', 'lewps_threads.thread_id','=','threads.id')
->leftjoin('threads_users', 'threads_users.thread_id','=','threads.id')
->where('threads.id', $id)
->wherein('threads_users.user_id', array(null, $users->id))
->groupBy(array('threads.id', 'users.username','threads_users.boosts'))
->take(1)
->get(array('threads.id','users.username','threads_users.boosts'));
请注意,我已经更改了线程。*' to' threads.id'在你的获取部分... tou必须通过calusule将你的选择列表与你的组同步。