通知

时间:2018-04-29 11:51:58

标签: laravel

我正在尝试使用laravel,我遇到了this文章。 我完全按照它,但由于某种原因,在Notification类中发送邮件时,他无法找到我在构造函数中声明的$ user变量。在构造函数中打印它时,它可以正常传递用户对象,但是当我想在toMail方法中访问它时,它由于某种原因而不存在。任何人都知道为什么&如何解决这个问题?

<?php

namespace App\Notifications;

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

class UserRegisteredSuccessfully extends Notification
{
    use Queueable;

    /**
     * @var User
     */
    protected $user;

    /**
     * Create a new notification instance.
     *
     * @param User $user
     */
    public function __construct(User $user)
    {
        $this->$user = $user;
        // printing here works
    }

    /**
     * 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)
    {
        // ERROR HERE (Undefined variable: user)
        $user = $this->$user;

        return (new MailMessage)
                    ->subject('Succesfully created new account')
                    ->greeting(sprintf('Hello %s', $user->username))
                    ->line('You have successfully registered to our system. Please activate your account.')
                    ->action('Click here', route('activate.user', $user->activation_code))
                    ->line('Thank you for using our application!');
    }

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

注册方法:

/**
 * Register new user
 * 
 * @param Request $request
 * @return User
 */
protected function register(Request $request) 
{
    $validatedData = $request->validate([
        'username' => 'required|string|max:255',
        'email' => 'required|string|email|max:255|unique:users',
        'password' => 'required|string|min:6|confirmed',
    ]);

    try {
        $validatedData['password'] = Hash::make(array_get($validatedData, 'password'));
        $validatedData['activation_code'] = str_random(30).time();
        $user = app(User::class)->create($validatedData);

    } catch(\Exception $ex) {
        logger()->error($ex);
        return redirect()->back()->with('message', 'Unable to create new user.');
    }

    $user->notify(new UserRegisteredSuccessfully($user));
    return redirect()->back()->with('message', 'Successfully created a new account. Please check your email and activate your account.');
}

提前致谢!

1 个答案:

答案 0 :(得分:1)

你犯了2个拼写错误:

在你的构造函数中:

$this->$user = $user;

应该是:

$this->user = $user;

toMail()方法中:

$user = $this->$user;

应该是:

$user = $this->user;

它不起作用的原因是因为您当前使用$user作为变量名,并且您没有将User对象的值赋给{{ 1}}。