【发布时间】:2017-04-18 16:39:47
【问题描述】:
我有 3 张桌子:posts、votes 和 users。我想排除当前用户已经投票的帖子(Auth::user()->id)。我应该如何做到这一点?我试过这段代码:
$user_id = Auth::user()->id;
$posti = Post::with('user')
->whereDoesntHave("votes")
->whereBetween('posts.created_at', array(Carbon::now()->subHours(48), Carbon::now()))
->orderBy('posts.created_at', 'DESC')
->orderByVotes()
->take(20)
->get();
【问题讨论】:
-
这将为您提供没有任何投票的帖子,而不是没有投票的用户。因此,对于那个 put where 条件,要使用用户 ID 在投票表中检查。
标签: mysql laravel eloquent laravel-5.4