当用户提交答案或时间在 laravel 8 中完成时,如何从数据库中获取下一条记录?

发布于 2025-01-11 07:23:08 字数 6291 浏览 0 评论 0原文

模型

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class Events extends Model
{
    use HasFactory;
    use SoftDeletes;
    public function EventQues()
    {
        return $this->hasMany(Questions::class, 'event_id')->limit(1);
    }
}

控制器

public function ActiveEvent()
    {
        $display = Events::where('status', 1)->with('EventQues')->first();
        // $next = Questions::where('id', '>',$request->id)->where('is_deleted',0)->first('id');
        return view('frontend.index', compact('display'));
    }
    // function for front send answer value to DB
    public function getData(Request $request)
    {
        $value = $request->all();
        return response()->json($value, 200);
    }

视图

@extends('frontend.master')
@section('title', 'Home')
@section('content')
    {{-- top menu Start --}}
    <nav class="navbar navbar-expand-lg navbar-dark bg-dark">
        <div class="container">
            <a class="navbar-brand text-uppercase font-weight-bold" href="#">Logo</a>
            <div class="col-2">
                <h5 class=" dropdown-toggle text-light" id="navbarDropdownMenuLink" data-toggle="dropdown"
                    aria-haspopup="true" aria-expanded="false">{{ Auth::user()->name }}</h5>
                <div class="dropdown-menu mt-1" aria-labelledby="navbarDropdownMenuLink">
                    <a class="dropdown-item" href="{{ route('twitch.logout') }}">Log Out</a>
                </div>
            </div>
        </div>
    </nav> {{-- top menu end --}}

    <div class="container-fluid">
        <div class="row">
            <div class="card shadow col-lg-8 col-md-8 col-sm-12 m-4">
                <div class="card-body ">
                    <div id="twitch-embed"></div>
                </div>
            </div>
            <div class="card shadow col-lg-3 col-md-3 col-sm-12 m-4">
                <div class="card-body">
                    <div id="list-example" class="list-group mt-2">
                        @if (!$display)
                            <h2 class="font-weight-bold mt-5" id="Nothing">Currently No Event Is Active</h2>
                        @else
                            <h3 class="font-weight-bold p-2 text-light text-center bg-primary">{{ $display->title }}</h3>
                            <p class="text-justify">{{ $display->description }}</p>
                            {{-- Displaying questions of Active event from database --}}
                            <h4 class="text-center p-2">Time :<span id="timer"> </span></h4>
                            @foreach ($display->EventQues as $data)
                            <div id="quesDiv"></div>
                            <div id="quesDiv1">
                                    <h5 id="question">{{ $data->question }}</h5>
                                    <form >
                                        @csrf
                                        <div class="btn btn-group-toggle d-flex flex-column" id="radioDiv"
                                            data-toggle="buttons">
                                            <label class="btn btn-secondary mb-2">
                                                <input type="radio" name="options" id="option_a1" class="answer-check-box"
                                                    autocomplete="off" data-value="1"> Yes
                                            </label>
                                            <label class="btn btn-secondary">
                                                <input type="radio" name="options" id="option_a2" class="answer-check-box"
                                                    autocomplete="off" data-value="2"> NO
                                            </label>
                                        </div>
                                    </form>
                                </div>
                            @endforeach
                        @endif
                    </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
@endsection
@section('scripts')
    @include('frontend.script')
@endsection

Script.js

$('#option_a1,#option_a2').click(function() {
            if (options = $("input[type='radio']:checked")) {
                val = options.attr('data-value');
            } else if (options = $("input:checkbox:not(:checked)")) {
                val = "";
            }
        });
            let interval = setInterval(req, 1000);
            function req() {

                $.ajaxSetup({
                    headers: {
                        'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                    }
                });

                if (val || timeleft < '0') {
                    $.ajax({
                        type: "post",
                        url: "{{ route('ajax') }}",
                        data: {
                            'id': '{{ $data->id }}',
                            'value': val
                        },
                        success: function(data) {
                            $('#quesDiv1').hide();
                            $('#quesDiv').html('Next Question');
                        },
                        error: function(data) {
                            console.log('error!!')
                        }
                    });
               var timeOut = setTimeout(interval, 1000);
                }
                // clearInterval(interval);
            }
     

我正在制作一个应用程序,在事件和事件之间建立一对多关系问题。当事件处于活动状态时,与该事件相关的问题一次显示一个,当用户提交答案时,然后从数据库中获取下一个问题。我为此使用了 Ajax 调用。

Model

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class Events extends Model
{
    use HasFactory;
    use SoftDeletes;
    public function EventQues()
    {
        return $this->hasMany(Questions::class, 'event_id')->limit(1);
    }
}

Controller

public function ActiveEvent()
    {
        $display = Events::where('status', 1)->with('EventQues')->first();
        // $next = Questions::where('id', '>',$request->id)->where('is_deleted',0)->first('id');
        return view('frontend.index', compact('display'));
    }
    // function for front send answer value to DB
    public function getData(Request $request)
    {
        $value = $request->all();
        return response()->json($value, 200);
    }

view

@extends('frontend.master')
@section('title', 'Home')
@section('content')
    {{-- top menu Start --}}
    <nav class="navbar navbar-expand-lg navbar-dark bg-dark">
        <div class="container">
            <a class="navbar-brand text-uppercase font-weight-bold" href="#">Logo</a>
            <div class="col-2">
                <h5 class=" dropdown-toggle text-light" id="navbarDropdownMenuLink" data-toggle="dropdown"
                    aria-haspopup="true" aria-expanded="false">{{ Auth::user()->name }}</h5>
                <div class="dropdown-menu mt-1" aria-labelledby="navbarDropdownMenuLink">
                    <a class="dropdown-item" href="{{ route('twitch.logout') }}">Log Out</a>
                </div>
            </div>
        </div>
    </nav> {{-- top menu end --}}

    <div class="container-fluid">
        <div class="row">
            <div class="card shadow col-lg-8 col-md-8 col-sm-12 m-4">
                <div class="card-body ">
                    <div id="twitch-embed"></div>
                </div>
            </div>
            <div class="card shadow col-lg-3 col-md-3 col-sm-12 m-4">
                <div class="card-body">
                    <div id="list-example" class="list-group mt-2">
                        @if (!$display)
                            <h2 class="font-weight-bold mt-5" id="Nothing">Currently No Event Is Active</h2>
                        @else
                            <h3 class="font-weight-bold p-2 text-light text-center bg-primary">{{ $display->title }}</h3>
                            <p class="text-justify">{{ $display->description }}</p>
                            {{-- Displaying questions of Active event from database --}}
                            <h4 class="text-center p-2">Time :<span id="timer"> </span></h4>
                            @foreach ($display->EventQues as $data)
                            <div id="quesDiv"></div>
                            <div id="quesDiv1">
                                    <h5 id="question">{{ $data->question }}</h5>
                                    <form >
                                        @csrf
                                        <div class="btn btn-group-toggle d-flex flex-column" id="radioDiv"
                                            data-toggle="buttons">
                                            <label class="btn btn-secondary mb-2">
                                                <input type="radio" name="options" id="option_a1" class="answer-check-box"
                                                    autocomplete="off" data-value="1"> Yes
                                            </label>
                                            <label class="btn btn-secondary">
                                                <input type="radio" name="options" id="option_a2" class="answer-check-box"
                                                    autocomplete="off" data-value="2"> NO
                                            </label>
                                        </div>
                                    </form>
                                </div>
                            @endforeach
                        @endif
                    </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
@endsection
@section('scripts')
    @include('frontend.script')
@endsection

Script.js

$('#option_a1,#option_a2').click(function() {
            if (options = $("input[type='radio']:checked")) {
                val = options.attr('data-value');
            } else if (options = $("input:checkbox:not(:checked)")) {
                val = "";
            }
        });
            let interval = setInterval(req, 1000);
            function req() {

                $.ajaxSetup({
                    headers: {
                        'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                    }
                });

                if (val || timeleft < '0') {
                    $.ajax({
                        type: "post",
                        url: "{{ route('ajax') }}",
                        data: {
                            'id': '{{ $data->id }}',
                            'value': val
                        },
                        success: function(data) {
                            $('#quesDiv1').hide();
                            $('#quesDiv').html('Next Question');
                        },
                        error: function(data) {
                            console.log('error!!')
                        }
                    });
               var timeOut = setTimeout(interval, 1000);
                }
                // clearInterval(interval);
            }
     

I am making an app where I have made one to many relation between events and questions. When an event is active questions related to this event are displayed one at a time and when a user submits an answer then fetch the next question from Database. And I was using Ajax call for this.

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文