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

ezykj2lf  于 2023-11-20  发布在  其他
关注(0)|答案(4)|浏览(128)

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


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

  1. <?php
  2. namespace App;
  3. use Illuminate\Contracts\Auth\MustVerifyEmail;
  4. use Illuminate\Foundation\Auth\User as Authenticatable;
  5. use Illuminate\Notifications\Notifiable;
  6. class User extends Authenticatable implements MustVerifyEmail
  7. {
  8. use Notifiable;
  9. /**
  10. * The attributes that are mass assignable.
  11. *
  12. * @var array
  13. */
  14. protected $fillable = [
  15. 'first_name', 'last_name', 'email', 'password', 'permissions'
  16. ];
  17. /**
  18. * The attributes that should be hidden for arrays.
  19. *
  20. * @var array
  21. */
  22. protected $hidden = [
  23. 'password', 'remember_token',
  24. ];
  25. /**
  26. * The attributes that should be cast to native types.
  27. *
  28. * @var array
  29. */
  30. protected $casts = [
  31. 'email_verified_at' => 'datetime',
  32. ];
  33. }

字符串
这里是web.php

  1. Auth::routes(['verify'=> true]);
  2. Route::prefix('student')->middleware(['auth', 'verified'])->group(function () {
  3. Route::get('dashboard', 'StudentController@dashboard');
  4. });

cbeh67ev

cbeh67ev1#

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

rwqw0loc

rwqw0loc2#

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

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

字符串
或中间件组:

  1. Route::group(['middleware' => ['auth']], function () {
  2. Route::get('admin/profile', function(){
  3. //your function here
  4. });
  5. });


Laravel官方文档

展开查看全部
2hh7jdfx

2hh7jdfx3#

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

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

字符串

euoag5mw

euoag5mw4#

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

  1. public function show(Request $request)
  2. {
  3. //login user
  4. if (auth()->user())
  5. {
  6. return $request->user()->hasVerifiedEmail()
  7. ? redirect($this->redirectPath())
  8. : view('auth.verify');
  9. }
  10. //guest
  11. else
  12. {
  13. return $request->user()
  14. ? redirect($this->redirectPath())
  15. : redirect('/login');
  16. }
  17. }

字符串

展开查看全部

相关问题