I make real-time notifications and stumbled upon this strange error. My model has a boot method that fires an event with a name SendNotificationData
(without a listener). It processes when a new notification appears.
Control controller
<?php
namespace App\Http\Controllers\Notification;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Models\Notification;
class NotificationController extends Controller
{
public function displayNotification()
{
$notification = new Notification();
$notification->EmployeeID = "EMP-00001";
$notification->NotificationText = "There is a new notification";
$notification->NotificationStatus = "unread";
$notification->NotificationType = "trial";
$notification->save();
}
}
The way to download the notification model:
public static function boot()
{
static::created(function ($data) {
event(new SendNotificationData($data));
});
parent::boot();
}
This is my event SendNotificationData
:
namespace App\Events;
use App\Events\Event;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
class SendNotificationData extends Event implements ShouldBroadcast
{
use SerializesModels;
public $new_notification_data;
public function __construct($new_notification_data)
{
$this->new_notification_data = $new_notification_data;
}
public function broadcastOn()
{
return ['new-notification'];
}
public function broadcastAs()
{
return 'private-send-new-notification';
}
}
On Javascript
var newNotificationChannel = pusher.subscribe('new-notification');
newNotificationChannel.bind("private-send-new-notification", function(data) {
addNotification(data);
});
function addNotification(data)
{
console.log(data);
$('.notification-link').closest('li').append('<a href="#">This is a sample notification!!!</a>');
}
, , . 404. ShouldBroadcast
, . , , . , -, , , .