INSIDECAMPUS TV
INSIDECAMPUS TV

Reputation: 13

laravel mail undefined $user variable

I am working on sending newly registered users details to admin via email notification and when I tried to register a user I'm getting this error:

Undefined variable $user

what I'm doing wrong here and how I can improve the code?

<?php

namespace App\Notifications;

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

class RegisteredUsers extends Notification
{
    use Queueable;

    /**
     * Create a new notification instance.
     *
     * @return void
     */

     public $user;
    public function __construct(User $user)
    {
        $this->user = $user;
    }

    /**
     * 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)->markdown('mail.registered.user');
    }

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

mail blade components

@component('mail::message')
# New Registered User Details

{{ $user->name }}<br>
{{ $user->username }}<br>
{{ $user->email }}<br>
{{ $user->password }}<br>
{{ $user->phone_number }}



@component('mail::button', ['url' => ''])
Check User Profile
@endcomponent

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

register controller

   public function store(Request $request)
    {
    $random =  str_pad(mt_rand(1,9999),4,'0',STR_PAD_LEFT);
       $data = $request->validate([
            'username' => ['required', 'string', 'max:255', 'unique:users'],
            'phone_number' => ['required', 'numeric', 'unique:users'],
            'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
            'password' => ['required', 'confirmed', Rules\Password::defaults()],
        ]);
        /* Get credentials from .env */
        $token = env("TWILIO_AUTH_TOKEN");
        $twilio_sid = env("TWILIO_SID");
        $twilio_verify_sid = env("TWILIO_VERIFY_SID");
        $twilio = new Client($twilio_sid, $token);
        $twilio->verify->v2->services($twilio_verify_sid)
            ->verifications
            ->create($data['phone_number'], "sms");
        $user = User::create([
            'username' => $request->username,
            'tribe_pin' => $random,
            'phone_number' => $request->phone_number,
            'email' => $request->email,
            'password' => Hash::make($request->password),
        ]);
        $admins = User::where('isAdmin', 1)->get();
        Notification::send($admins, new RegisteredUsers($user));
        return redirect()->route('verify')->with(['phone_number' => $data['phone_number']]);

Upvotes: 0

Views: 448

Answers (1)

Alberto
Alberto

Reputation: 12939

You have to pass the variable to the markdown method:

return (new MailMessage)->markdown('mail.registered.user', ['user' => $this->user]);

as you can see from here (check the markdown function)

Upvotes: 2

Related Questions