yussuf mussa
yussuf mussa

Reputation: 21

How to dispatch an event in laravel 8?

I am manually implementing user registration within my application and I have failed to understand this section of laravel 8 docs

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

I tried different approaches but in the end i failed to understand this and email is not being sent Here is my registration code public function storeUser(Request $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
        ]);
        
        
}

My question is where do I dispatch this event the documentation is saying?

Here is the top of my 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;

and here is the mail code in env

MAIL_MAILER=smtp
MAIL_HOST=kokayazanzibar.com
MAIL_PORT=465
[email protected]
MAIL_PASSWORD=ienteredmypasswordhere
MAIL_ENCRYPTION=tls
[email protected]
MAIL_FROM_NAME="${APP_NAME}"

Here is 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()
    {
        //
    }
}

Upvotes: 1

Views: 10255

Answers (2)

yussuf mussa
yussuf mussa

Reputation: 21

I did not implement the interface on my User Model. It was

class User extends Authenticatable 
{

I thought I put it well but I was missing this implementation and was supposed to be like this

class User extends Authenticatable implements MustVerifyEmail
    {

and now its working.

Upvotes: 1

James
James

Reputation: 16339

You would dispatch this after the user is created in your application.

In your case:

$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));

Upvotes: 2

Related Questions