如何获取总和id少

时间:2018-05-02 06:47:02

标签: laravel fetch where

寻找一些帮助如何从数据库中获取数据,同样的questionId总和小于2?我正在获取用户ID不一样的数据,但我需要获取其中questionId不相同的地方。因为现在当我在我的表中回答关于userId的问题时,我可以在下一次调查中获得相同的userId,其中问题是不同的,这就是为什么我需要检查userId和questionId是否已经存在于数据库中

任何想法如何做到这一点?

我的控制员:

public function viewSurvey($id)
{
    $object = DB::table('question')
        ->where('survey_id', '=', $id)
        ->where('name', '!=', NULL)
        ->get();

    $teamName = DB::table('survey')->where('surveyId', '=', $id)
        ->join('team','survey.teamId','=','team.teamId')
        ->join('company','company.id','=','team.companyID')
        ->get();

    $company = Auth::user()->company;

    $date = Survey::where('surveyId', '=', $id)->get();
    $teams = Auth::user()->teams;

    $checkUserTeam = DB::table('teammembersall')
        ->where('UserId', '=', Auth::user()->id)
        ->get();

    $members = Survey::where('surveyId', '=', $id)
    ->join('team', 'team.teamId', '=', 'survey.teamId')
    ->join('teammembersall', 'teammembersall.TeamId', '=', 'team.TeamId')
    ->join('users', 'users.id', '=', 'teammembersall.UserId')
    ->select('users.*')
    ->whereNotExists(function ($query) {
        $query->select(DB::raw(1))
            ->from('answer')
            ->whereRaw('answer.answerAboutUserId = users.id')
            ->andWhere('answer.questionId', '<', 2);
    })
    ->get();

    $questions = DB::table('answer')->get();

    return view('survey_details', ['object' => $object, 'date' => $date, 'teams' => $teams, 'members' => $members, 'questions' => $questions, 'checkUserTeam' => $checkUserTeam, 'teamName' => $teamName, 'company' => $company ]);
}

我的观点:

<html>
<head>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
</head>
<body>
    <div class="container">
        <hr>
        <div class="row">
            <div class="col-md-2">
            </div>
            <div class="col-md-8">
                    <br>
                    <div style="display:none">
                    {{  $dates = date('Y-m-d H:i:s') }}
                    </div>
                        <div class="">
                            <img src={{url('')}} width="100%" height="auto" alt=""/>
                            <div class="" style="display:none">
                                @foreach($teamName as $tname)
                                    <h4>Team name:</h4>
                                    <p>{{ $tname->nameCompany}}</p>
                                @endforeach
                                {{ $company->nameCompany }}
                            </div>
                        </div>

                        <div class="container-survey-logo">
                            <img src={{url('/img/survey-banner.jpg')}} width="100%" height="auto" alt=""/>
                            <div class="text-block-survey-date">
                                @foreach($date as $dat)
                                <h4>End date:</h4>
                                <p>{{ $dat->ended_at}}</p>
                                @endforeach
                            </div>
                        </div>
                        <div style="display:none;">
                            @foreach($questions as $quest)
                                <p>{{ $quest->answerAboutUserId}}</p>
                            @endforeach
                        </div>
                        @if($dat->ended_at > $dates )
                        @if( $tname->nameCompany == $company->nameCompany )
                            <div class="survey-theme">
                                @foreach($members as $memb)
                                    @if($memb->id != Auth::user()->id)
                                    <form action="/confirmSurveyAnswers" method="post">
                                        {{csrf_field()}}
                                        <br>
                                        <div class="well well-lg">
                                        <h5>
                                            Questions about member:
                                            <h2><input style="border:none;background:none" name="surveyName" value="{{ $memb->name}}" readonly></h2>
                                           <input style="border:none;background:none;display:none" name="surveyUserDataId" value="{{ $memb->id}}" readonly>
                                         </h5>
                                        <br>
                                        @foreach($object as $object_each)
                                            <input style="font-size:20px;" type="text" class="form-control" id="exampleInputAnswer" name="questionName[{{$object_each->id}}]" value="{!! $object_each->name !!}" readonly>
                                            <div class="survey-questions">
                                                <label class="radio-inline-text">
                                                    Not agree -
                                                </label>
                                                <label class="radio-inline">
                                                    <input type="checkbox" value="1" name="QuestionsAnswers[{{$object_each->id}}]" >1
                                                </label>
                                                <label class="radio-inline">
                                                    <input type="checkbox" value="2" name="QuestionsAnswers[{{$object_each->id}}]" >2
                                                </label>
                                                <label class="radio-inline">
                                                    <input type="checkbox" value="3" name="QuestionsAnswers[{{$object_each->id}}]" >3
                                                </label>
                                                <label class="radio-inline">
                                                    <input type="checkbox" value="4" name="QuestionsAnswers[{{$object_each->id}}]" >4
                                                </label>
                                                <label class="radio-inline">
                                                    <input type="checkbox" value="5" name="QuestionsAnswers[{{$object_each->id}}]" >5
                                                </label>
                                                <label class="radio-inline">
                                                    <input type="checkbox" value="6" name="QuestionsAnswers[{{$object_each->id}}]" >6
                                                </label>
                                                <label class="radio-inline">
                                                    <input type="checkbox" value="7" name="QuestionsAnswers[{{$object_each->id}}]" >7
                                                </label>
                                                <label class="radio-inline">
                                                    <input type="checkbox" value="8" name="QuestionsAnswers[{{$object_each->id}}]" >8
                                                </label>
                                                <label class="radio-inline">
                                                    <input type="checkbox" value="9" name="QuestionsAnswers[{{$object_each->id}}]" >9
                                                </label>
                                                <label class="radio-inline">
                                                    <input type="checkbox" value="10" name="QuestionsAnswers[{{$object_each->id}}]" >10
                                                </label>
                                                <label class="radio-inline-text">
                                                    &nbsp;&nbsp; - Fully agree
                                                </label>
                                            </div>
                                            @endforeach
                                            <div class="im-done-button">
                                                <button type="submit" class="btn btn-primary">I'm Done</button>
                                            </div>
                                        </div>
                                        @endif
                                    </form>
                                    @endforeach
                                    <Br>
                                    @include('layouts.errors')
                            </div>
                        @else <h1>It's not your team survey</h1>  @endif
                        @else <h1>This survey ended</h1>  @endif
            </div>
            <div class="col-md-2">
            </div>
        </div>
        <hr>
    </div>

</body>
</html>

1 个答案:

答案 0 :(得分:0)

我找到了解决方案,在我的答案表中创建了一个新列:surveyId,在插入数据时,还插入了调查ID。 然后改变我的控制器方法来检查它:

$members = Survey::where('surveyId', '=', $id)
        ->join('team', 'team.teamId', '=', 'survey.teamId')
        ->join('teammembersall', 'teammembersall.TeamId', '=', 'team.TeamId')
        ->join('users', 'users.id', '=', 'teammembersall.UserId')
        ->select('users.*')
        ->whereNotExists(function ($query) use ($id) {
               $query->select(DB::raw(1))
                   ->from('answer')
                   ->whereRaw('answer.answerAboutUserId = users.id')
                   ->where('answer.surveyId', '=', $id);
        })
        ->get();