Skip to content

Latest commit

 

History

History

22-Notifications

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 

Notifications

STEP 1) Configure MailTrap

image

  • Select the Laravel 7+ Integration

image

  • Copy the connection values

image

  • in the env file, replace the email connection information with the mailtrap connection values

image

STEP 2) Create a new Event

  • Run the following command to create a new Notification
php artisan make:notification SendRegistrationEmail 

image

  • Add a protected property to receive the $user as parameter in the constructor
public $user;
public function __construct($user)
{
    $this->user = $user;
}

image

  • Replace the toMail() function with the following code
public function toMail($notifiable)
{
    return (new MailMessage)
         ->line('Dear '. $this->user->name . '. This is a notification to let you know that you were registered to our site.')
         ->action('You were succesfully registered' ., url('/'))
         ->line('Thank you for using our application!');
    }
  • Add database in the via() function return array
  return ['mail', 'database'];

image

STEP 3) Send Notification on the User creation

  • In the UserController.php add the following line in the store() function
$user->notify(new SendRegistrationEmail($user));

image

STEP 4) Create the Notification table Migration

  • Run the following command to create a database migration to create the notifications table
php artisan notification:table

image

  • Run the following command to run the migration and seed the database
php artisan migrate:refresh --seed