Laravel MustVerifyEmail 未发送电子邮件
Laravel MustVerifyEmail is not sending e-mail
我目前正在与 Laravel 5.7 合作一个项目。当我在我的 User
模型中实现 MustVerifyEmail
class 时,应用程序没有按预期发送电子邮件。案例是:
1) 我在此处遵循了 Laravel 文档:https://laravel.com/docs/5.7/verification
2) 我用https://mailtrap.io测试了功能,但是我没有收到任何来自应用程序的验证邮件。
3) 我尝试使用 Auth::routes()
而不是 Auth::routes(['verify' => true])
并且预期会出现错误,但没有发生错误,应用程序只是在用户注册后将我重定向到主页。
这是我的 User
模型:
<?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', 'phone_number', 'username', 'role'
];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'password', 'remember_token'
];
public function freelancer()
{
return $this->hasOne('App\Freelancer', 'freelancer_id', 'id');
}
public function employer()
{
return $this->hasOne('App\Employer', 'employer_id', 'id');
}
}
这是我在 RegisterController.php
中的 create()
函数:
protected function create(array $data)
{
if ($data['role'] == 'freelancer') {
$user = User::create([
'first_name' => $data['first_name'],
'last_name' => $data['last_name'],
'email' => $data['email'],
'phone_number' => $data['phone_number'],
'username' => $data['username'],
'password' => Hash::make($data['password']),
'role' => $data['role'],
])->freelancer()->create([
'overview_description' => '',
]);
} else {
$user = User::create([
'first_name' => $data['first_name'],
'last_name' => $data['last_name'],
'email' => $data['email'],
'phone_number' => $data['phone_number'],
'username' => $data['username'],
'password' => Hash::make($data['password']),
'role' => $data['role'],
])->employer()->create([
'overview_description' => '',
'number_of_employees' => 0,
]);
}
return $user;
}
我的Freelancer
和Employer
模特:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Freelancer extends Model
{
protected $table = 'freelancer';
public $timestamps = false;
protected $fillable = [
'overview_description'
];
}
--
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Employer extends Model
{
protected $table = 'employer';
public $timestamps = false;
protected $fillable = [
'overview_description', 'number_of_employees'
];
}
我认为 RegisterController
有问题,但我不确定它在哪里。有没有逻辑错误?
您忘记在您的用户模型中使用 Illuminate\Auth\MustVerifyEmail
特征,此 特征 定义了负责发送验证电子邮件的 sendEmailVerificationNotification
方法。
use Illuminate\Contracts\Auth\MustVerifyEmail as MustVerifyEmailContract; // this is an interface
use Illuminate\Auth\MustVerifyEmail; // this the trait
class User extends Authenticatable implements MustVerifyEmail
{
use MustVerifyEmail, Notifiable;
// all the other code goes here
}
我更新了RegisterController.php
中的create
函数后问题解决了,如下:
protected function create(array $data)
{
$user = User::create([
'first_name' => $data['first_name'],
'last_name' => $data['last_name'],
'email' => $data['email'],
'phone_number' => $data['phone_number'],
'username' => $data['username'],
'password' => Hash::make($data['password']),
'role' => $data['role'],
]);
return $user;
}
至于Freelancer
和Employer
实例的创建过程,我放在邮箱验证之后了。
我目前正在与 Laravel 5.7 合作一个项目。当我在我的 User
模型中实现 MustVerifyEmail
class 时,应用程序没有按预期发送电子邮件。案例是:
1) 我在此处遵循了 Laravel 文档:https://laravel.com/docs/5.7/verification
2) 我用https://mailtrap.io测试了功能,但是我没有收到任何来自应用程序的验证邮件。
3) 我尝试使用 Auth::routes()
而不是 Auth::routes(['verify' => true])
并且预期会出现错误,但没有发生错误,应用程序只是在用户注册后将我重定向到主页。
这是我的 User
模型:
<?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', 'phone_number', 'username', 'role'
];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'password', 'remember_token'
];
public function freelancer()
{
return $this->hasOne('App\Freelancer', 'freelancer_id', 'id');
}
public function employer()
{
return $this->hasOne('App\Employer', 'employer_id', 'id');
}
}
这是我在 RegisterController.php
中的 create()
函数:
protected function create(array $data)
{
if ($data['role'] == 'freelancer') {
$user = User::create([
'first_name' => $data['first_name'],
'last_name' => $data['last_name'],
'email' => $data['email'],
'phone_number' => $data['phone_number'],
'username' => $data['username'],
'password' => Hash::make($data['password']),
'role' => $data['role'],
])->freelancer()->create([
'overview_description' => '',
]);
} else {
$user = User::create([
'first_name' => $data['first_name'],
'last_name' => $data['last_name'],
'email' => $data['email'],
'phone_number' => $data['phone_number'],
'username' => $data['username'],
'password' => Hash::make($data['password']),
'role' => $data['role'],
])->employer()->create([
'overview_description' => '',
'number_of_employees' => 0,
]);
}
return $user;
}
我的Freelancer
和Employer
模特:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Freelancer extends Model
{
protected $table = 'freelancer';
public $timestamps = false;
protected $fillable = [
'overview_description'
];
}
--
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Employer extends Model
{
protected $table = 'employer';
public $timestamps = false;
protected $fillable = [
'overview_description', 'number_of_employees'
];
}
我认为 RegisterController
有问题,但我不确定它在哪里。有没有逻辑错误?
您忘记在您的用户模型中使用 Illuminate\Auth\MustVerifyEmail
特征,此 特征 定义了负责发送验证电子邮件的 sendEmailVerificationNotification
方法。
use Illuminate\Contracts\Auth\MustVerifyEmail as MustVerifyEmailContract; // this is an interface
use Illuminate\Auth\MustVerifyEmail; // this the trait
class User extends Authenticatable implements MustVerifyEmail
{
use MustVerifyEmail, Notifiable;
// all the other code goes here
}
我更新了RegisterController.php
中的create
函数后问题解决了,如下:
protected function create(array $data)
{
$user = User::create([
'first_name' => $data['first_name'],
'last_name' => $data['last_name'],
'email' => $data['email'],
'phone_number' => $data['phone_number'],
'username' => $data['username'],
'password' => Hash::make($data['password']),
'role' => $data['role'],
]);
return $user;
}
至于Freelancer
和Employer
实例的创建过程,我放在邮箱验证之后了。