laravel 5.6未定义的变量:用户在尝试发送电子邮件时

时间:2018-07-31 13:30:37

标签: php laravel laravel-5.2

我正在尝试使用laravel 5.6中的Notification类发送电子邮件,这是我的新手。

我试图传递用户和预订信息,但是每次获得以下信息:

未定义变量:用户

这是我的successEmail.php:

  <?php

namespace BOOK_DONATION\Notifications;

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

class successEmail extends Notification
{
    use Queueable;
    public $user;
    public $book;

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

    /**
     * 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)
        ->greeting('Dear'.$user->name.'thak you for creating donation for '.$book->name);    
    }

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

这是调用类successEmail的函数:

 public function doBook(Request $request){
            $validatedData = $request->validate([
                'title' => 'string|required|max:255',
                'Author'=> 'string|required|max:255',
                'Cover_image' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:2048',
                'Book_description'=>'string|required|max:255',

            ]);
        $path= $request->Cover_image->store('images');
        $uid=Auth::user()->id;
        $country=Auth::user()->country;
        $city=Auth::user()->city;
        $book=Book::create(['title'=>$request->input('title'),
                       'Author'=>$request->input('Author'),
                       'user_id'=>$uid,
                       'country'=>$country,
                        'city' =>$city,
                        'Book_description'=>$request->input('Book_description'),
                        'path'=>$path,
                        ]);
         $user = Auth::user();

         $user->notify(new successEmail($user,$book));

        return redirect('/donation/create')->with('status', 'Thank you for your donation');
    }

如您所见,我将用户和book变量传递给了构造函数,并将它们声明为公共变量,那么我想念的是什么?

4 个答案:

答案 0 :(得分:7)

这些变量在函数范围中不存在,但它们确实作为类属性存在,因此应将其视为此类。

return (new MailMessage)
    ->greeting('Dear '.$this->user->name.' thank you for creating donation for '.$this->book->name);

答案 1 :(得分:5)

您需要调用$ this-> user来访问类范围内的变量。

 /**
     * Get the mail representation of the notification.
     *
     * @param  mixed  $notifiable
     * @return \Illuminate\Notifications\Messages\MailMessage
     */
    public function toMail($notifiable)
    {
        return (new MailMessage)
        ->greeting('Dear'.$this->user->name.'thak you for creating donation for '.$this->book->name);    
    }

答案 2 :(得分:1)

您应该尝试以下操作:

youtube.com

答案 3 :(得分:0)

那件事也发生在我身上。在toMail函数中,您需要重新声明像这样的变量

  

$ user = $ this-> user;

然后您像这样调用变量

  ->line('user:'.''.$user,'')