mar*_*ark 0 php laravel laravel-5.2
我正在尝试使用laravel 5.6中的Notification类发送电子邮件,我是新手.
我试图传递用户和书籍信息,但每次我得到以下内容:
未定义的变量:user
这是我的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 [
//
];
}
}
Run Code Online (Sandbox Code Playgroud)
这是调用类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');
}
Run Code Online (Sandbox Code Playgroud)
如你所见,我将用户和book变量传递给构造函数并将其声明为public,那么我缺少什么?
这些变量在函数范围中不存在,但它们确实作为类属性存在,因此应将它们视为这样.
return (new MailMessage)
->greeting('Dear '.$this->user->name.' thank you for creating donation for '.$this->book->name);
Run Code Online (Sandbox Code Playgroud)
小智 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);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
321 次 |
| 最近记录: |