Ad

Laravel Custom Verification Notification Error

- 1 answer

I'm trying to update the verification email notification in Laravel. I tried to generate verification link in AppServiceProvider then passed the link to a notification class but later it gave me an error that 'undefined property ::$view'.

AppServiceProvider

/**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        VerifyEmail::toMailUsing(function ($notifiable) {
             $verificationUrl = URL::temporarySignedRoute(
                'verification.verify',
                Carbon::now()->addMinutes(config('auth.verification.expire', 60)),
                [
                    'id' => $notifiable->getKey(),
                    'hash' => sha1($notifiable->getEmailForVerification()),
                ]
            );

            return new EmailVerification($verificationUrl);
        });
    }

VerificationEmail

<?php

namespace App\Notifications;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;

class EmailVerification extends Notification implements ShouldQueue
{
    use Queueable;

    public $verificationUrl;

    /**
     * Create a new notification instance.
     *
     * @return void
     */
    public function __construct($verificationUrl)
    {
        $this->verificationUrl = $verificationUrl;
    }

    /**
     * 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)
    {
        $verificationUrl = $this->verificationUrl;

        return (new MailMessage)
                        ->subject('Please verify your email')
                        ->markdown('emails.verification', ['url' => $verificationUrl]);
    }

    /**
     * Get the array representation of the notification.
     *
     * @param  mixed  $notifiable
     * @return array
     */
    public function toArray($notifiable)
    {
        return [
            //
        ];
    }
}

Associated markdown view to the Notification.

@component('mail::message')
# Email verification - {{ config('app.name') }}

Your registration on our application <b> {{ config('app.name') }} </b> was successfull. Kindly click the button below to verify your email address.

@component('mail::button', ['url' => $url])
Verify Email
@endcomponent

Thanks,<br>
{{ config('app.name') }}
@endcomponent

The error I'm receiving

** ErrorException Undefined property: App\Notifications\EmailVerification::$view **

Actual error message

Ad

Answer

It's very well explained in this tutorial.
https://brabeum.com/2019/10/customise-the-laravel-user-verification-email/

You should first override in your user model the default function that send the notification when the user register.

/app/User.php

/**
 * Override the default function that send a notification to verify
 * the email after a new user register.
 *
 */
  public function sendEmailVerificationNotification()
  {
     $this->notify(new Notifications\UserVerificationEmail);
  }

Then to create a custom notification:

/app/Notifications/EmailVerification.php.

<?php

namespace App\Notifications;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\URL;

class UserVerificationEmail 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('Please verify your email address')
        ->markdown(
            'emails.userverify', 
            [
               'url' => $this->verificationUrl($notifiable),
               'notifiable' => $notifiable,
            ]
        );
    }

    /**
     * Get the array representation of the notification.
     *
     * @param  mixed  $notifiable
     * @return array
     */
    public function toArray($notifiable)
    {
        return [
            //
        ];
    }

    /*
   * Build the verification URL
   *
   * @return URL
   */
   protected function verificationUrl($notifiable)
   {
      return URL::temporarySignedRoute(
         'verification.verify',
         Carbon::now()->addMinutes(
            Config::get('auth.verification.expire', 60)),
              [
                'id' => $notifiable->getKey(),
                'hash' => sha1($notifiable->getEmailForVerification()),
              ]     
         ); 
   }
}

and then the mailable template:

/resources/views/emails/userverify.blade.php

@component('mail::message')
# Welcome {{ $notifiable->name }}

Before you can use this tutorial system you must verify your email address.

@component('mail::button', ['url' => $url])
Brabeum Verify Email Address Tutorial
@endcomponent

If you did not create an account, no further action is required.
Thanks,

{{ config('app.name') }} Team

@component('mail::subcopy')
If you’re having trouble clicking the "Verify Email Address" button, copy and paste the URL below into your web browser: {{ $url }} 
@endcomponent

@endcomponent
Ad
source: stackoverflow.com
Ad