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

Php 检查数据库中是否存在记录,发送电子邮件并重定向到Laravel 7.x中的输入令牌页面,php,mysql,laravel,laravel-artisan,Php,Mysql,Laravel,Laravel Artisan,我是拉威尔的新手。我正在开发一个应用程序,让参加培训的人可以下载证书的软拷贝。这就是我想要实现的流程 索引页面显示三种输入形式 证书编号(证书编号) 证书类型(证书类型) 会员电邮(会员电邮) 提交时,如果记录存在,则发送一封包含令牌的电子邮件,并发送到下一页,在该页中,您将令牌发送到您的电子邮件 现在我的挑战是,如何检查数据库以验证一条记录是否满足所有要求 下面是我的表格 <form method="POST" action="{{ route('

我是拉威尔的新手。我正在开发一个应用程序,让参加培训的人可以下载证书的软拷贝。这就是我想要实现的流程

  • 索引页面显示三种输入形式

    • 证书编号(证书编号)
    • 证书类型(证书类型)
    • 会员电邮(会员电邮)
    提交时,如果记录存在,则发送一封包含令牌的电子邮件,并发送到下一页,在该页中,您将令牌发送到您的电子邮件

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

下面是我的表格

<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>
下面是我的函数

public function checkExist(Request $request)
{
    $this->validate($request, [
        'cert_no'       => 'required',
        'cert_type'     => 'required',
        'member_email'  => 'required|email'
    ]);

    $cert_no        = $request->Input('cert_no');
    $cert_type      = $request->Input('cert_type');
    $member_email   = $request->Input('member_email');

    $certificate = Certificate::where('cert_no', '=', $cert_no)->where('cert_type', 'LIKE', $cert_type)->where('member_email', '=', $member_email)->get();

    if (count($certificate) > 0)
    {
        return 'Yes';
    }
    else
    {
        return 'No';
    }
}
虽然数据库中有一条符合要求的记录,但返回的结果是“否”

[更新1]请注意,我必须使用所有三个输入进行验证的原因是,在某些培训中,一个证书编号可以有两个证书类型。因此,需要检查数据库中是否存在证书号、证书类型和成员电子邮件。还要注意的是,我已经使用普通的PHP$query完成了这项工作,并且可以正常工作

[更新2]我从上面的函数中删除了->where('cert\u type','LIKE',$cert\u type),它工作正常。所以现在它会检查证书号和会员电子邮件是否存在。但我也需要使用证书类型。我之所以使用LIKE,是因为cert_type下存储的数据是逗号分隔的数组。由于field member_电子邮件是唯一的,因此同一电子邮件的两条记录不能存在,但一封电子邮件可以有多种类型的证书

[更新3]因此,它终于开始工作了。我非常感谢所有的努力和贡献。我遗漏了“%”符号,因此更改了代码形式

$certificate = Certificate::where('cert_no', '=', $cert_no)->where('cert_type', 'LIKE', $cert_type)->where('member_email', '=', $member_email)->get();


在laravel中,您已经有了执行此操作的validatio。 看看这个


使用exists,您可以检查特定数据是否在数据库中可用。

这样做对您有用吗

public function checkExist(Request $request)
{
    $data = $request->validate([
        'cert_no'       => 'required',
        'cert_type'     => 'required',
        'member_email'  => 'required|email'
    ]);

    $exists = Certificate::where($data)->exists();

    if ($exists)
    {
        return 'Yes';
    }
    else
    {
        return 'No';
    }
}

如果不符合要求,则执行dd($data)并查看来自前端的数据是否符合您的要求。

您可以使用以下代码检查有效查询

dump($certificate->toSql(), $certificate->getBindings());

Do dd($证书);检查内容以确保它与数据库表中的记录匹配。是的。当我在函数中有->where('cert\u type','LIKE',$cert\u type)时,返回了空项[]。但当我去掉那条线时,它就起作用了。但我还需要使用cert_typeCorrection进行验证:如果($存在),对吗?所以我尝试了这个,它仍然返回“否”。使用dd返回数据库中存在的输入值,即使我选择了错误的证书类型。是,是,抱歉。。。打错了。它的
$存在
。刚刚更新过。这很奇怪。能否转储
$data
的内容以及数据库中这一匹配行的内容?这返回了一个错误集合::toSql不存在
public function checkExist(Request $request)
{
    $data = $request->validate([
        'cert_no'       => 'required',
        'cert_type'     => 'required',
        'member_email'  => 'required|email'
    ]);

    $exists = Certificate::where($data)->exists();

    if ($exists)
    {
        return 'Yes';
    }
    else
    {
        return 'No';
    }
}
dump($certificate->toSql(), $certificate->getBindings());