如何通过广播解决 laravel 通知? Laravel 5.3
How to solve laravel notification with broadcast ? Laravel 5.3
我的控制器是这样的:
public function add($param)
{
...
$message = Message::create([
...
'created_at' => Carbon::now(),
'updated_at' => Carbon::now()
]);
Notification::send($notify_to, new MessageInsert($message));
dd($message);
return $message;
}
我的 MessageInsert 是这样的:
<?php
namespace App\Notifications;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
class MessageInsert extends Notification implements ShouldBroadcast, ShouldQueue
{
use Queueable;
private $data;
public function __construct($data)
{
$this->data = $data;
}
public function via($notifiable)
{
return ['broadcast','database','mail'];
}
public function toArray($notifiable)
{
return [
'id' => $this->data->id,
...
];
}
public function toMail($notifiable)
{
return (new MailMessage)
->greeting('Hello!')
->line('...')
->action('...')
->line('Thank you!');
}
}
如果像这样:
return ['broadcast','database','mail'];
dd($message);
的结果是错误的
但如果像这样:
return ['database','mail'];
dd($message);
的结果是$message
的数组
为什么我使用广播时出现错误?
您是否尝试添加 toBroadcast 函数?
public function toBroadcast($notifiable) {
return new BroadcastMessage([ 'invoice_id' => $this->invoice->id, 'amount' => $this->invoice->amount, ]);
}
我的控制器是这样的:
public function add($param)
{
...
$message = Message::create([
...
'created_at' => Carbon::now(),
'updated_at' => Carbon::now()
]);
Notification::send($notify_to, new MessageInsert($message));
dd($message);
return $message;
}
我的 MessageInsert 是这样的:
<?php
namespace App\Notifications;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
class MessageInsert extends Notification implements ShouldBroadcast, ShouldQueue
{
use Queueable;
private $data;
public function __construct($data)
{
$this->data = $data;
}
public function via($notifiable)
{
return ['broadcast','database','mail'];
}
public function toArray($notifiable)
{
return [
'id' => $this->data->id,
...
];
}
public function toMail($notifiable)
{
return (new MailMessage)
->greeting('Hello!')
->line('...')
->action('...')
->line('Thank you!');
}
}
如果像这样:
return ['broadcast','database','mail'];
dd($message);
的结果是错误的
但如果像这样:
return ['database','mail'];
dd($message);
的结果是$message
为什么我使用广播时出现错误?
您是否尝试添加 toBroadcast 函数?
public function toBroadcast($notifiable) {
return new BroadcastMessage([ 'invoice_id' => $this->invoice->id, 'amount' => $this->invoice->amount, ]);
}