Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/246.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何进行实时laravel 5.3通知_Php_Laravel_Laravel 5.3 - Fatal编程技术网

Php 如何进行实时laravel 5.3通知

Php 如何进行实时laravel 5.3通知,php,laravel,laravel-5.3,Php,Laravel,Laravel 5.3,我正在使用Laravel 5.3通知 我成功地创建了数据库通知,以及如何向目标用户显示所有或未读的通知 例如,这是我的通知类,通知notif在课程中使用她的注册状态: class NewChangeRegisterStatusNotif extends Notification { use Queueable; public function __construct ($status_id, $course_id) { $this->status

我正在使用Laravel 5.3通知

我成功地创建了数据库通知,以及如何向目标用户显示所有或未读的通知

例如,这是我的通知类,通知notif在课程中使用她的注册状态:

class NewChangeRegisterStatusNotif extends Notification
{
    use Queueable;


    public function __construct ($status_id, $course_id)
    {
        $this->status = CourseUserStatus::findOrFail($status_id);
        $this->course = Course::findOrFail($course_id)->first(['title']);
    }


    public function via ($notifiable)
    {
        return ['database'];
    }


    public function toMail ($notifiable)
    {
        return (new MailMessage)
            ->line('The introduction to the notification.')
            ->action('Notification Action', 'https://laravel.com')
            ->line('Thank you for using our application!');
    }


    public function toArray ($notifiable)
    {
        return [
            'message' =>
                ' Your Status in ' .
                '<strong class="text-default">' . $this->course->title . '</strong>'
                . ' Course has changed to  ' .
                '<strong class="text-' . $this->status->label . '">' . $this->status->title . '</strong>';

            ,
            'action'  => '#'
        ];
    }
}
类NewChangeRegisterStatusNotif扩展通知
{
使用可排队;
公共功能构造($status\u id,$course\u id)
{
$this->status=CourseUserStatus::findOrFail($status\u id);
$this->course=course::findOrFail($course_id)->first(['title']);
}
公共职能通过($notifiable)
{
返回['database'];
}
托梅尔的公共职能(应呈报)
{
返回(新邮件)
->第行(“通知简介”)
->动作('通知动作','https://laravel.com')
->行(“感谢您使用我们的应用程序!”);
}
公共职能toArray($Notified)
{
返回[
“消息”=>
“您在中的状态”。
“。$this->course->title。””
“课程改为”。
“”.$此->状态->标题。”;
,
“操作”=>“#”
];
}
}
要显示我在刀片模板中编写的所有通知,请执行以下操作:

<ul class="menu">
    @foreach($AuthUser->unreadNotifications as $notif)
        <li>
            <a href="{{$notif->data['action']}}" data-notif-id="{{$notif->id}}">
                {!! $notif->data['message'] !!}
            </a>
        </li>
    @endforeach
</ul>
    @foreach($AuthUser->unreadNotifications as$notif)
  • @endforeach
一切都很好,但现在我想让它成为实时的

我知道我可以使用和。我安装推桥并遵循指导


但我不知道如何在我的通知中使用它?如何在
via()
方法中定义它?我该怎么做?

现在,您创建了一个后端来管理通知

您在视图中所做的一切都是显示来自服务器端呈现的通知(即,在创建新页面时)

如果您确实需要实时通知(我假设您需要,因为您提到了pusher),那么您需要将通知从服务器推送到应用程序的客户端代码

按照您提到的教程,推送服务器端使用以下代码完成:

$pusher->trigger( 'test-channel',
                  'test-event', 
                  array('text' => 'Preparing the Pusher Laracon.eu workshop!'));
在这里,你的laravel应用程序将推送一条消息。您所要做的就是设置客户端(javascript)以接收并显示它

这一部分可以使用以下javascript代码完成:

var pusher = new Pusher('{{env("PUSHER_KEY")}}');
var channel = pusher.subscribe('channel-name');
channel.bind('event-name', function(data) {
    // do something with the event data
});
现在由您来执行显示该通知所需的任何操作。它可以简单到一个console.log,也可以复杂到创建新的html元素并将它们添加到菜单中


很抱歉没有提出实际的代码,但我希望我能让您更清楚地了解这一点,这样您就可以继续了。

现在,您创建了一个后端来管理您的通知

您在视图中所做的一切都是显示来自服务器端呈现的通知(即,在创建新页面时)

如果您确实需要实时通知(我假设您需要,因为您提到了pusher),那么您需要将通知从服务器推送到应用程序的客户端代码

按照您提到的教程,推送服务器端使用以下代码完成:

$pusher->trigger( 'test-channel',
                  'test-event', 
                  array('text' => 'Preparing the Pusher Laracon.eu workshop!'));
在这里,你的laravel应用程序将推送一条消息。您所要做的就是设置客户端(javascript)以接收并显示它

这一部分可以使用以下javascript代码完成:

var pusher = new Pusher('{{env("PUSHER_KEY")}}');
var channel = pusher.subscribe('channel-name');
channel.bind('event-name', function(data) {
    // do something with the event data
});
现在由您来执行显示该通知所需的任何操作。它可以简单到一个console.log,也可以复杂到创建新的html元素并将它们添加到菜单中


很抱歉没有提出实际的代码,但我希望我能让你更清楚,这样你就可以继续了。

谢谢@Atrakeur,但我的主要问题是我必须在哪里调用
$pusher->trigger(“测试通道”…
?我正在寻找一种方法,使
NewChangeRegisterStatusNotif
通知可以自动完成所有后端工作。谢谢@Atrakeur,但我的主要问题是我必须在哪里调用
$pusher->trigger(“测试频道”…
?我正在寻找一种方法,使
NewChangeRegisterStatusNotif
通知可以自动完成所有后端工作。