can you explain how to send a email invitation when you add a new doctor ?

How we can send an email when you create another doctor , and also how to use the verification email for every new admin
1 Reply
tuto1902
tuto190216mo ago
You can take advantage of Events and model lifecycle hooks. Start by creating a new event:
php artisan make:event DoctorCreated
php artisan make:event DoctorCreated
Then, update the DoctorCreated class
<?php

namespace App\Events;

use App\Models\User;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;

class DoctorCreated
{
use Dispatchable, InteractsWithSockets, SerializesModels;

/**
* Create a new event instance.
*/
public function __construct(
public User $user
)
{}
}
<?php

namespace App\Events;

use App\Models\User;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;

class DoctorCreated
{
use Dispatchable, InteractsWithSockets, SerializesModels;

/**
* Create a new event instance.
*/
public function __construct(
public User $user
)
{}
}
Now, create an event listener class and a mailable class
php artisan make:listener SendWelcomeEmail --event=DoctorCreated
php artisan make:mail WelcomeEmail
php artisan make:listener SendWelcomeEmail --event=DoctorCreated
php artisan make:mail WelcomeEmail
This is how your SendWelcomeEmail listener class should look like
<?php

namespace App\Listeners;

use App\Events\DoctorCreated;
use App\Mail\WelcomeEmail;
use Illuminate\Support\Facades\Mail;

class SendWelcomeEmail
{

/**
* Handle the event.
*/
public function handle(DoctorCreated $event): void
{
if ($event->user->role->name == 'doctor') {
Mail::to($event->user->email)->send(new WelcomeEmail($event->user));
}
}
}
<?php

namespace App\Listeners;

use App\Events\DoctorCreated;
use App\Mail\WelcomeEmail;
use Illuminate\Support\Facades\Mail;

class SendWelcomeEmail
{

/**
* Handle the event.
*/
public function handle(DoctorCreated $event): void
{
if ($event->user->role->name == 'doctor') {
Mail::to($event->user->email)->send(new WelcomeEmail($event->user));
}
}
}
This should be the content of the WelcomeEmail mailable class
<?php

namespace App\Mail;

use App\Models\User;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Mail\Mailables;
use Illuminate\Mail\Mailables\Content;
use Illuminate\Mail\Mailables\Envelope;
use Illuminate\Queue\SerializesModels;

class WelcomeEmail extends Mailable
{
use Queueable, SerializesModels;

/**
* Create a new message instance.
*/
public function __construct(
public User $user
)
{}

/**
* Get the message envelope.
*/
public function envelope(): Envelope
{
return new Envelope(
from: new Mailables\Address('[email protected]', 'Pet Clinic Admin'),
subject: "Welcome {$this->user->name}",
);
}

/**
* Get the message content definition.
*/
public function content(): Content
{
return new Content(
view: 'emails.welcome',
);
}
}
<?php

namespace App\Mail;

use App\Models\User;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Mail\Mailables;
use Illuminate\Mail\Mailables\Content;
use Illuminate\Mail\Mailables\Envelope;
use Illuminate\Queue\SerializesModels;

class WelcomeEmail extends Mailable
{
use Queueable, SerializesModels;

/**
* Create a new message instance.
*/
public function __construct(
public User $user
)
{}

/**
* Get the message envelope.
*/
public function envelope(): Envelope
{
return new Envelope(
from: new Mailables\Address('[email protected]', 'Pet Clinic Admin'),
subject: "Welcome {$this->user->name}",
);
}

/**
* Get the message content definition.
*/
public function content(): Content
{
return new Content(
view: 'emails.welcome',
);
}
}
Create the resources\views\emails\welcome.blade.php file and edit the content as you see fit
<h1>Welcome {{$user->name}}</h1>
<h1>Welcome {{$user->name}}</h1>
And finally, update the User class with this method
protected static function booted(): void
{

static::created(function ($user) {
event(new DoctorCreated($user));
});
}
protected static function booted(): void
{

static::created(function ($user) {
event(new DoctorCreated($user));
});
}
Let me know if this works for you It's been a few days now. I'll mark this as solved for the time being.
Want results from more Discord servers?
Add your server