检查数据库中是否存在记录,发送电子邮件并重定向到 Laravel 7.x 中的输入令牌页面

我是 Laravel 的新手。我正在构建一个应用程序,参加培训的人可以下载他们的证书软拷贝。这就是我想要实现的流程

  • 索引页显示三个输入的形式

    提交时,如果记录存在,请发送一封包含令牌的电子邮件,并路由到下一页,您在其中提供发送到电子邮件的令牌。

    • 证书编号 (cert_no)

    • 证书类型(cert_type)

    • 会员邮箱 (member_email)

现在我的挑战是,如何检查数据库以验证一条记录满足所有要求?

下面是我的表格

<form method="POST" action="{{ route('checkExist') }}" enctype="multipart/form-data" data-form-validate="true" novalidate="novalidate">

    @csrf

    <div class="card bg-white shadow">

        <div class="card-body pt-2">

            <div class="input-group">

                <label>Certificate No.</label>

                <input type="text" name="cert_no" class="input input-md input-green input-block" value="{{ old('cert_no') }}" data-rule-required="true" data-msg-required="* Field cannot be empty" required>

            </div>


            <div class="input-group">

                <label>Choose Type</label>

                <select name="cert_type" class="input input-md input-green input-block" data-rule-required="true" data-msg-required="* Select an option" required>

                    @if ($errors->any())

                        <option value="{{ old('cert_type') }}">{{ old('cert_type') }}</option>

                    @endif


                    <option value="">---Select Option---</option>

                    <option value="Certificate 1">Certificate 1</option>

                    <option value="Certificate 2">Certificate 2</option>

                </select>

            </div>


            <div class="input-group">

                <label>Email</label>

                <input type="email" name="member_email" class="input input-md input-green input-block" value="{{ old('member_email') }}" data-rule-required="true" data-msg-required="* Field cannot be empty" required>

            </div>


            <div class="input-group mt-1">

                <input type="submit" class="btn btn-lg btn-cipm btn-block" value="Get Access">

            </div>

        </div>

    </div>

</form>


潇湘沐
浏览 37回答 3
3回答

Qyouu

在 Laravel 中,您已经有了执行此操作的验证。看看这个。https://laravel.com/docs/7.x/validation#rule-exists使用存在,您可以检查数据库中是否存在特定数据。

摇曳的蔷薇

这样的事情对你有用吗?public function checkExist(Request $request){&nbsp; &nbsp; $data = $request->validate([&nbsp; &nbsp; &nbsp; &nbsp; 'cert_no'&nbsp; &nbsp; &nbsp; &nbsp;=> 'required',&nbsp; &nbsp; &nbsp; &nbsp; 'cert_type'&nbsp; &nbsp; &nbsp;=> 'required',&nbsp; &nbsp; &nbsp; &nbsp; 'member_email'&nbsp; => 'required|email'&nbsp; &nbsp; ]);&nbsp; &nbsp; $exists = Certificate::where($data)->exists();&nbsp; &nbsp; if ($exists)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; return 'Yes';&nbsp; &nbsp; }&nbsp; &nbsp; else&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; return 'No';&nbsp; &nbsp; }}如果没有,dd($data)看看前端传来的数据是否满足你的要求。

POPMUISE

您可以使用以下代码来检查有效查询dump($certificate->toSql(),&nbsp;$certificate->getBindings());
打开App,查看更多内容
随时随地看视频慕课网APP