如何使用 Laravel 8 验证 MySQL 表中是不是存在哈希码

Posted

技术标签:

【中文标题】如何使用 Laravel 8 验证 MySQL 表中是不是存在哈希码【英文标题】:How do I verify a hash code exists in MySQL table using Laravel 8如何使用 Laravel 8 验证 MySQL 表中是否存在哈希码 【发布时间】:2021-12-18 00:34:57 【问题描述】:

我希望能够在具有给定哈希码的用户填写注册表之前验证表中是否存在具有特定哈希码的确认行。

我知道哈希码存在,因为它在数据库中。但是如果哈希码不存在,应用程序会重定向到它应该出现的页面,并输出哈希码不存在的消息。

这曾经在 Laravel 5 中工作,但似乎自 Laravel 8 以来实现已经改变。

这是我的代码:

use App\Http\Controllers\Site\Guest\newMembership\RegistrationForm;

...

Route::get('registration/hashCode', [RegistrationForm::class, 'registrationForm'])->name('registration');

    Route::post('registration', [RegistrationForm::class, 'registrationProcessing'])->name('registration_data');

我的控制器 RegistrationForm.php 有代码:

namespace App\Http\Controllers\Site\Guest\newMembership;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;

class RegistrationForm extends Controller

    public function registrationForm($hashCode)
    
        // Check if this $hashCode exists at all, otherwise redirect to referral request home page.
        $hash_exists = (new \App\Http\Controllers\Site\Guest\newMembership\Form)->hashExists($hashCode);

        // In the event the number of times it occurs is zero
        if($hash_exists < 1)
        
            // Tell user the referral link is non-existent.
            session()->flash('membershipInfo', 'Your code does not exist.');

            // Take this user to the referral request welcome page
            return redirect()->route('membership.index');
        

        // Assign new variable name to hash code
        $referralCode = $hashCode;

        return view('membership.registration', [
            'referralCode' => $referralCode,
        ]);
    

表单控制器有代码:

// Import database model
use App\Models\Site\Guest\Referralrequestapplication;

...

   /**
     * Count number of entries with given ID
     */
    public function hashExists($hashCode)
    
        //DB::table('referralrequestapplications')
        Referralrequestapplication::where(['blocked', 0], ['image_reference', $hashCode])->count();
    

型号代码:

namespace App\Models\Site\Guest;

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

class Referralrequestapplication extends Model

    use HasFactory;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'blocked', 'review', 'reviewer', 'firstname', 'surname', 'phone', 'email', 'nin', 'facebook', 'twitter', 'instagram', 'state', 'city', 'neighbourhood', 'address', 'image_reference', 'created_at', 'updated_at',
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $table = 'referralrequestapplications';

迁移架构有代码:

Schema::create('referralrequestapplications', function (Blueprint $table) 
            $table->bigIncrements('id');
            $table->boolean('blocked')->default(0);
            $table->boolean('reviewed')->default(0);
            $table->string('reviewer')->nullable();

            $table->string('firstname', 30)->nullable();
            $table->string('surname', 30)->nullable();
            $table->string('phone', 30)->nullable();
            $table->string('email', 70)->nullable();
            $table->string('nin', 20)->nullable();
            $table->string('facebook', 70)->nullable();
            $table->string('twitter', 70)->nullable();
            $table->string('instagram', 70)->nullable();
            $table->string('state', 40)->nullable();
            $table->string('city', 40)->nullable();
            $table->string('neighbourhood', 40)->nullable();
            $table->string('address', 200)->nullable();
            $table->string('image_reference', 200)->nullable();
            $table->timestamps();

        );

【问题讨论】:

【参考方案1】:

我通过更改表单控制器中的代码来解决此问题:

Referralrequestapplication::where(['blocked', 0], ['image_reference', $hashCode])->count();

收件人:

Referralrequestapplication::where('blocked', 0)->where('mage_reference', $hashCode)->exists();

我还在 RegistrationForm 控制器中更改了以下内容:

if($hash_exists < 1)

收件人:

if($hash_exists = 0)

【讨论】:

您确定=if($hash_exists = 0) 中的正确运算符吗?

以上是关于如何使用 Laravel 8 验证 MySQL 表中是不是存在哈希码的主要内容,如果未能解决你的问题,请参考以下文章

laravel jwtAuth 验证凭据

如何在 laravel 5.8 或 6 中制作通用自定义包进行身份验证。*

laravel 8:验证唯一的多个表

Laravel 8 验证输入,用于排除不在表中的数据,必须为大写,并格式化 id

Laravel 4 - 如何在视图中访问 MySQL 数据库

laravel 8 : 注册后发送邮件验证 laravel