For this purpose you can use Laravel’s own triggers, I’m referring to Observers. One of the advantages is that you are already using something ready, which is part of the core of Laravel. Not to mention of course Observers allows you to fire automatic email in other situations as well.
First we create the file: App Observers Observaruser.php with the following content:
<?php
namespace App\Observers;
use App\User;
use App\Notifications\NotificarNovoUsuario;
class ObservarUser
{
/**
* Listen to the User created event.
*
* @param User $user
* @return void
*/
public function created(User $user)
{
$user->notify(new NotificarNovoUsuario());
}
/**
* Listen to the User deleting event.
*
* @param User $user
* @return void
*/
public function deleting(User $user)
{
//
}
}
Note that in addition to created you have other possible methods that can be used following the same logic.
And now we’ve created a:
php artisan make:notification NotificarNovoUsuario
This will generate the file App Notifications Notificarnovousuario.php with the following content:
<?php
namespace App\Notifications;
use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
class NotificarNovoUsuario extends Notification
{
use Queueable;
/**
* Create a new notification instance.
*
* @return void
*/
public function __construct()
{
//
}
/**
* Get the notification's delivery channels.
*
* @param mixed $notifiable
* @return array
*/
public function via($notifiable)
{
return ['mail'];
}
/**
* Get the mail representation of the notification.
*
* @param mixed $notifiable
* @return \Illuminate\Notifications\Messages\MailMessage
*/
public function toMail($notifiable)
{
return (new MailMessage)
->subject('Seja bem-vindo ao ' . config('app.name'))
->line('É com grande alegria que te damos as boas vindas!')
->action('Clique aqui para acessar ', url('/'))
->line('Qualquer dúvida nossa equipe está sempre a disposição!');
}
/**
* Get the array representation of the notification.
*
* @param mixed $notifiable
* @return array
*/
public function toArray($notifiable)
{
return [
//
];
}
}
All I did was modify the method toMail to insert my own message.
And finally you edit your App Providers Appserviceprovider.php and changes the method boot for:
public function boot()
{
User::observe(ObservarUser::class);
}
Nice guy that you taught me. I’m very grateful. Thank you very much.
– Natan Melo
More where this conf. . config('app.name') ?
– Natan Melo
Inside the file. env you have a line with the name of your app, I do not remember the name of the head parameter, but I think it is the first line, this conf(? app.name') can be overwritten there. Or even inside the config/app.php file
– Fábio Jânio
Well I took a look and that’s right. vlw Gave it right for what I want. Obg even
– Natan Melo
And you live next door in Brasilia , I’m here from Luziania. Vlw
– Natan Melo
Ball show then. Anything we are around. I climbed a project in Laravel on my Github if you want to check: github.com/fabiojaniolima/Webupload
– Fábio Jânio
add to share some ideas. fb.com/Natan.support
– Natan Melo