Laravel - event causing 404 error

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
{  
    /**
     * Trigger event to display notifications. This displays 404 error page
     *
     * @return none
     */
    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:

/**
 * Handle booting of model.
 *
 * @var string
 */
 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;

    /**
     * Create a new event instance.
     *
     * @param $notification_data
     * @return void
     */
    public function __construct($new_notification_data)
    {
        $this->new_notification_data = $new_notification_data;
    }

    /**
     * Get the channels the event should be broadcast on.
     *
     * @return array
     */
    public function broadcastOn()
    {
        return ['new-notification'];
    }

    /**
     * Customize event name.
     *
     * @return array
     */
    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);
}); //This gives me no error in the console and the 404 error still shows up even if i remove this..

function addNotification(data)
{
    console.log(data);
    $('.notification-link').closest('li').append('<a href="#">This is a sample notification!!!</a>');
}

, , . 404. ShouldBroadcast , . , ​​, . , -, , , .

+4
1

, $incrementing , ​​ false true. laravel .

+1

Source: https://habr.com/ru/post/1686286/


All Articles