检查Auth用户是否投票,然后对民意调查进行计数?

发布于 2025-01-23 03:26:19 字数 1533 浏览 0 评论 0原文

我遇到了一个阻碍我前进的问题。

到目前为止,我已经尝试获得民意调查的数量:

 public function user_dashboard(){

        $activepolls = DB::table('larapoll_polls')
                ->selectRaw('count(*) As total')
                ->join('larapoll_options', 'larapoll_polls.id', '=', 'larapoll_options.poll_id')
                ->join('larapoll_votes', 'larapoll_votes.option_id', '=', 'larapoll_options.id')
                ->where('larapoll_votes.user_id', auth()->user()->id)
                ->count();

        return view('user_dashboard',compact('activepolls'));
    }

到目前为止,我认为下面的功能会帮助我,但我不确定。

public function hasVoted($poll_id)
    {
        $poll = Poll::findOrFail($poll_id);

        if ($poll->canGuestVote()) {
            $result = DB::table('larapoll_polls')
                ->selectRaw('count(*) As total')
                ->join('larapoll_options', 'larapoll_polls.id', '=', 'larapoll_options.poll_id')
                ->join('larapoll_votes', 'larapoll_votes.option_id', '=', 'larapoll_options.id')
                ->where('larapoll_votes.user_id', auth()->user()->id)
                ->where('larapoll_options.poll_id', $poll_id)->count();
            return $result !== 0;
        }

        return $this->options()->where('poll_id', $poll->id)->count() !== 0;
    }

简而言之,我在我的网站上制作了一个民意测验箱,我想在该盒子的右角显示民意测验计数。

  1. 我需要用户未投票的民意调查计数。
  2. 如果用户对民意调查进行投票,则应从数量上删除。

如果有人能提供帮助,回应将非常感谢..

谢谢。

I encountered an issue that blocks my way forward.

I have tried getting count of polls as of now as shown below:

 public function user_dashboard(){

        $activepolls = DB::table('larapoll_polls')
                ->selectRaw('count(*) As total')
                ->join('larapoll_options', 'larapoll_polls.id', '=', 'larapoll_options.poll_id')
                ->join('larapoll_votes', 'larapoll_votes.option_id', '=', 'larapoll_options.id')
                ->where('larapoll_votes.user_id', auth()->user()->id)
                ->count();

        return view('user_dashboard',compact('activepolls'));
    }

As of Now I am thinking that this below function would have help me but i am not sure.

public function hasVoted($poll_id)
    {
        $poll = Poll::findOrFail($poll_id);

        if ($poll->canGuestVote()) {
            $result = DB::table('larapoll_polls')
                ->selectRaw('count(*) As total')
                ->join('larapoll_options', 'larapoll_polls.id', '=', 'larapoll_options.poll_id')
                ->join('larapoll_votes', 'larapoll_votes.option_id', '=', 'larapoll_options.id')
                ->where('larapoll_votes.user_id', auth()->user()->id)
                ->where('larapoll_options.poll_id', $poll_id)->count();
            return $result !== 0;
        }

        return $this->options()->where('poll_id', $poll->id)->count() !== 0;
    }

Simply i had made a poll box in my website and i want to show the count of polls in right corner of that box.

  1. i need the count of poll in which user has not voted.
  2. if user votes on poll it should remove from count as well.

If anyone can help , the response would much appreciated..

Thanks..

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

尬尬 2025-01-30 03:26:19

我已经通过以下代码解决了问题...

$activepolls = DB::table('larapoll_polls')
                ->selectRaw('count(*) As total')
                ->join('larapoll_options', 'larapoll_polls.id', '=', 'larapoll_options.poll_id')
                ->join('larapoll_votes', 'larapoll_votes.option_id', '=', 'larapoll_options.id')
                ->where('larapoll_votes.user_id', auth()->user()->id)
                ->count();

$polls = Poll::count();

$unvotedpolls = ($polls - $activepolls);

在($ unvotedPolls)中,我得到了验证用户未投票的民意调查。

I have Solved The issue by following code...

$activepolls = DB::table('larapoll_polls')
                ->selectRaw('count(*) As total')
                ->join('larapoll_options', 'larapoll_polls.id', '=', 'larapoll_options.poll_id')
                ->join('larapoll_votes', 'larapoll_votes.option_id', '=', 'larapoll_options.id')
                ->where('larapoll_votes.user_id', auth()->user()->id)
                ->count();

$polls = Poll::count();

$unvotedpolls = ($polls - $activepolls);

in ($unvotedpolls) i got the count of polls in which auth user has not voted...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文