gpt4 book ai didi

Laravel 检查用户电子邮件是否已验证

转载 作者:行者123 更新时间:2023-12-05 08:38:16 24 4
gpt4 key购买 nike

您好,我想检查用户电子邮件是否仅在 Controller 中的一个函数中验证,我不想在中间件或路由中设置检查,如下所示:

 public function __construct()
{
$this->middleware('verified');
}

因为 guest 可以访问 Controller ,所以 Controller 中只有一个功能(创建)需要验证电子邮件和用户登录我该怎么做,谢谢

        public function create()
{

// checking if authenticated but need to check if email verified also
if (Auth::check()) {

// Get the currently authenticated user...
$user = Auth::user();

// get the list of states
$states = State::orderBy('name', 'asc')->get();
// get all cities by state_id
$state_id = '1';

$cities = City::where('state_id', $state_id)->orderBy('name', 'asc')->get();

return view('pages.create_product', ['user' => $user, 'states' => $states, 'cities' => $cities]);
} else {
return redirect()->route('login');
}
}

最佳答案

我建议您在 web.php 路由文件中这样做:

Route::get('example/create', ExampleController@create)->middleware('verified');

或者在 Controller 构造函数中执行,但是对于像这样的特定方法:

 public function __construct()
{
$this->middleware('verified')->only('create');
}

如果您真的想在 Controller 方法中执行此操作,您可以使用此代码进行检查。

$user = Auth::user();

$user->hasVerifiedEmail();

并且不要忘记在您的User 模型中包含MustVerifyEmail 接口(interface)。

<?php

namespace App;

use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;

class User extends Authenticatable implements MustVerifyEmail
{
use Notifiable;

// ...
}

关于Laravel 检查用户电子邮件是否已验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63570109/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com