laravel 对成员函数hasVerifiedEmail()的调用(null)

ezykj2lf  于 12个月前  发布在  其他
关注(0)|答案(4)|浏览(96)

我正在使用Laravel 7,我试图验证我的电子邮件,我已经遵循了文档中提到的所有步骤,但我仍然得到这个错误,请我解决这个错误,谢谢


的数据
我在这里添加了用户模型代码

<?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;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'first_name', 'last_name', 'email', 'password', 'permissions'
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];

    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = [
        'email_verified_at' => 'datetime',
    ];
}

字符串
这里是web.php

Auth::routes(['verify'=> true]);

Route::prefix('student')->middleware(['auth', 'verified'])->group(function () {
    Route::get('dashboard', 'StudentController@dashboard');
});

cbeh67ev

cbeh67ev1#

$request发送一个 null 值,因为您需要登录(身份验证)才能获得$user的示例

rwqw0loc

rwqw0loc2#

确保您的路由上有auth中间件
将中间件转换为路由:

Route::get('admin/profile', function () {
    //
})->middleware('auth');

字符串
或中间件组:

Route::group(['middleware' => ['auth']], function () {
    Route::get('admin/profile', function(){
        //your function here
    });
});


Laravel官方文档

2hh7jdfx

2hh7jdfx3#

我也有这个问题,然后我意识到用户没有登录或会话已经丢失。你应该重新登录以避免这个问题,或者你应该把一个if检查用户是否登录或没有。这个问题不会发生。像下面的代码片段

if (auth()->check()) {
    // The user is enter code herelogged in...
    if(!auth()->user()->hasVerifiedEmail()){
      
    }
}

字符串

euoag5mw

euoag5mw4#

只有登录的用户可以访问函数hasVerifiedEmail(),因为你得到的响应:调用成员函数hasVerifiedEmail()为null,要解决这个问题,你必须自定义显示功能VerificationController.php

public function show(Request $request)
    {
//login user
        if (auth()->user())
        {
            return $request->user()->hasVerifiedEmail()
                ? redirect($this->redirectPath())
                : view('auth.verify');
        }
//guest
        else
        {
            return $request->user()
                ? redirect($this->redirectPath())
                : redirect('/login');
        }

    }

字符串

相关问题