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

laravel 5.6 Undefined variable: user when trying to send email

我正在尝试使用 laravel 5.6 中的通知 class 发送电子邮件,我是新手。

我正在尝试传递用户和图书信息,但每次我都收到以下信息:

未定义变量:用户

这是我的 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 [
            //
        ];
    }
}

这里是调用 class 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');
    }

如您所见,我将 user 和 book 变量传递给构造函数并将它们声明为 public 那么我缺少什么?

这些变量不存在于函数作用域中,但它们作为 class 属性存在,因此它们应该被这样对待。

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

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

 /**
     * 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);    
    }

你应该试试这个:

use Auth;

public function toMail($notifiable)
    {

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

我也遇到过这种情况。在 toMail 函数中,您需要像这样重新声明变量

$user= $this->user;

然后你像这样调用变量

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