Php 未定义变量:$用于通知的消息
我正在尝试在对话中出现新消息时向用户发出通知 出于某种原因,它说它是通知中未定义的变量 代码如下: MessageController.phpPhp 未定义变量:$用于通知的消息,php,laravel,Php,Laravel,我正在尝试在对话中出现新消息时向用户发出通知 出于某种原因,它说它是通知中未定义的变量 代码如下: MessageController.php $message=$conversation->messages()->创建([ 'user_id'=>auth()->id(), “body”=>请求(“body”), 'conversation\u id'=>请求('conversation\u id') ]); //创建消息时,通知对话的所有用户 foreach($message->convers
$message=$conversation->messages()->创建([
'user_id'=>auth()->id(),
“body”=>请求(“body”),
'conversation\u id'=>请求('conversation\u id')
]);
//创建消息时,通知对话的所有用户
foreach($message->conversation->users as$messageConversationUser){
//如果(auth()->user()->id!==$message->user->id){
//dd($信息);
$messageConversationUser->notify(newyouhavenewmessage($message));
//}
}
YouHaveNewMessage.php
class YouHaveNewMessage extends Notification
{
use Queueable;
protected $message;
/**
* Create a new notification instance.
*
* @return void
*/
public function __construct($message)
{
$this->message = $message;
}
/**
* Get the notification's delivery channels.
*
* @param mixed $notifiable
* @return array
*/
public function via($notifiable)
{
return ['database'];
}
/**
* Get the mail representation of the notification.
*
* @param mixed $notifiable
* @return \Illuminate\Notifications\Messages\MailMessage
*/
public function toMail($notifiable)
{
return (new MailMessage)
->line('The introduction to the notification.')
->action('Notification Action', url('/'))
->line('Thank you for using our application!');
}
/**
* Get the array representation of the notification.
*
* @param mixed $notifiable
* @return array
*/
public function toArray($notifiable)
{
return [
'message' => $message->user->name . ' left a new message in ' . $message->conversation->title,
'link' => $this->message->path()
];
}
}
我并没有真正使用正在发生的事情——它在
dd()
上返回消息,但抛出一个未定义的变量错误。有人能帮忙吗?在YouHaveNewMessage
类中,toArray()方法中有$message变量。看起来您正在使用局部作用域,但此方法中没有声明$message
变量
您需要将其用作$this->message
public function toArray($notifiable)
{
return [
'message' => $this->message->user->name . ' left a new message in ' . $this->message->conversation->title,
'link' => $this->message->path()
];
}
在
YouHaveNewMessage
类中,toArray()方法中有$message variabe。看起来您正在使用局部作用域,但此方法中没有声明$message
变量
您需要将其用作$this->message
public function toArray($notifiable)
{
return [
'message' => $this->message->user->name . ' left a new message in ' . $this->message->conversation->title,
'link' => $this->message->path()
];
}
我认为您需要将
$message
变量更改为其他变量,因为laravel会自动将$message
变量提供给您的所有电子邮件模板。我尝试了$newMessage和$conversation,但不起作用,我仍然得到错误你确定$message->user->name不是toArray函数中的$this->message->user->name吗?谢谢Osama,我没有提到传递的对象--它应该是$this->message->user->name我想你需要将$message
变量更改为其他变量,因为laravel会自动将$message
变量提供给您的所有电子邮件模板。我尝试了$newMessage和$conversation,但没有成功,我仍然收到错误消息。您确定在toArray函数中$message->user->name不是$this->message->user->name吗?谢谢Osama,我没有引用传递的对象——它应该是$this->message->user->name