如何在 laravel 8 中发送事件?

How to dispatch an event in laravel 8?

我在我的应用程序中手动实现用户注册,但我未能理解 laravel 8 文档的这一部分

If you are manually implementing registration within your application instead of using a starter kit, you should ensure that you are dispatching the Illuminate\Auth\Events\Registered event after a user's registration is successful:

use Illuminate\Auth\Events\Registered;

event(new Registered($user));

我尝试了不同的方法,但最后我没能理解这一点,也没有发送电子邮件 这是我的注册码 public 函数存储用户(请求 $request){

        $validated = $request->validate([
            'first_name' => 'required',
            'last_name' => 'required',
            'email'  => 'required|email:rfc,dns',
            'password' => ['required','confirmed', Password::min(8)],
            'phone_number' => 'required'
            
        ]);
        
        $registeredDetails = User::create([
            'first_name' => $request->first_name,
            'last_name' => $request->last_name,
            'email' => $request->email,
            'password' => Hash::make($request->password),
            'phone_number' => $request->phone_number
        ]);
        
        
}

我的问题是我应该在哪里发送文档中提到的这个事件?

这是我的 UserControler 的顶部

namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use Illuminate\Validation\Rules\Password;
use App\Models\User;
use Illuminate\Support\Facades\Hash;
use Illuminate\Auth\Events\Registered;

这里是 env

中的邮件代码
MAIL_MAILER=smtp
MAIL_HOST=kokayazanzibar.com
MAIL_PORT=465
MAIL_USERNAME=demo@kokayazanzibar.com
MAIL_PASSWORD=ienteredmypasswordhere
MAIL_ENCRYPTION=tls
MAIL_FROM_ADDRESS=demo@kokayazanzibar.com
MAIL_FROM_NAME="${APP_NAME}"

这里是 EventServiceProvider

    class EventServiceProvider extends ServiceProvider
{
    /**
     * The event listener mappings for the application.
     *
     * @var array
     */
    protected $listen = [
        Registered::class => [
            SendEmailVerificationNotification::class,
        ],
    ];

    /**
     * Register any events for your application.
     *
     * @return void
     */
    public function boot()
    {
        //
    }
}

您将在您的应用程序中创建用户后分派它。

你的情况:

$validated = $request->validate([
    'first_name' => 'required',
    'last_name' => 'required',
    'email'  => 'required|email:rfc,dns',
    'password' => ['required','confirmed', Password::min(8)],
    'phone_number' => 'required'        
]);
        
$registeredDetails = User::create([
    'first_name' => $request->first_name,
    'last_name' => $request->last_name,
    'email' => $request->email,
    'password' => Hash::make($request->password),
    'phone_number' => $request->phone_number
]);
    
event(new Registered($registeredDetails));

我没有在我的用户模型上实现接口。 这是

class User extends Authenticatable 
{

我以为我说得很好,但我错过了这个实现,应该是这样的

class User extends Authenticatable implements MustVerifyEmail
    {

现在可以正常使用了。