Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/redis/2.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/3/apache-spark/5.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
Redis Laravel 5.4:侦听通知不';你不能和我一起工作_Redis_Notifications_Laravel 5.4_Broadcasting_Laravel Echo - Fatal编程技术网

Redis Laravel 5.4:侦听通知不';你不能和我一起工作

Redis Laravel 5.4:侦听通知不';你不能和我一起工作,redis,notifications,laravel-5.4,broadcasting,laravel-echo,Redis,Notifications,Laravel 5.4,Broadcasting,Laravel Echo,我无法使此代码在以下()中工作: 这里是我的代码: app.js: CreateTeckitNotify: <?php namespace App\Notifications\Ticket; use Illuminate\Bus\Queueable; use Illuminate\Notifications\Notification; use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Notifications\Mes

我无法使此代码在以下()中工作:

这里是我的代码:
app.js:

CreateTeckitNotify:

<?php

namespace App\Notifications\Ticket;

use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Messages\BroadcastMessage;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use Illuminate\Queue\SerializesModels;

use App\Models\Ticket\Ticket;



class CreateTicketNotify extends Notification  implements ShouldQueue, ShouldBroadcast
{
    use Queueable, SerializesModels;


    public $ticket;

    /**
     * Create a new notification instance.
     *
     * @return void
     */
    public function __construct($ticket)
    {
        $this->ticket = $ticket;
    }

    /**
     * Get the notification's delivery channels.
     *
     * @param  mixed  $notifiable
     * @return array
     */
    public function via($notifiable)
    {
        return ['broadcast'];
    }

    /**
     * Get the mail representation of the notification.
     *
     * @param  mixed  $notifiable
     * @return \Illuminate\Notifications\Messages\MailMessage
     */
    public function toMail($notifiable)
    {
        $url = url('/ticket/'.$this->ticket->id);
        return (new MailMessage)
                    ->subject('New Ticket Created')
                    ->greeting('Hello, '.$this->ticket->user->lastname.' !')
                    ->line('A new ticket has been created')
                    ->action('View Ticket', $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 [
            'ticket_id' => $this->ticket->id,                     
        ];
    }

    // public function toDatabase($notifiable)
    // {
    //     return [

    //     ];
    // }

    public function toBroadcast($notifiable)
    {

        return new BroadcastMessage([
            'ticket' => $this->ticket
        ]);
    }


}
频道及路线:

Broadcast::channel('App.User.{id}', function ($user, $id) {
    return (int) $user->id === (int) $id;
});

Broadcast::channel('ticket.{id}', function ($user, $id) {
    // $ticket = App\Models\Ticket\Ticket::find($ticketID);
    // return $ticket->category->users->has(Auth::id());
    // if(Auth::id()==1)        
    // if($id == 0)
    //  return false;
    // else
        return true;
    $ticket = App\Models\Ticket\Ticket::find($id);

    if (in_array(Auth::id(), $ticket->category->users->pluck('id')->toArray()) )
        return true;
    else
        return false;

    // return (int) $user->id !== (int) App\Models\Ticket\Ticket::find($ticketID)->user->id;
    // else
    //  return false;
});
用户模型:

<?php

namespace App;

use Illuminate\Notifications\Notifiable;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Spatie\Permission\Traits\HasRoles;
use Spatie\MediaLibrary\HasMedia\HasMediaTrait;
use Spatie\MediaLibrary\HasMedia\Interfaces\HasMedia;
use Auth;

class User extends Authenticatable implements HasMedia
{
    use Notifiable, HasRoles, HasMediaTrait;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'lastname', 'firstname', 'email', 'password', 'active',
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];




    public function tickets()
    {
        return $this->hasMany('App\Models\Ticket\Ticket');
    } 

    public function receivesBroadcastNotificationsOn()
    {
        return 'users.'.$this->id;
    }
}
如您所见,用户2已加入频道,并已触发通知,但使用此选项不会显示任何内容:

window.Echo.private('App.User.' + window.Laravel.user.id)               
    .notification((notification) => {
        console.log(notification);
        // toastr.info(' has created new ticket !! ', 'info');        
    });

请帮助我……

我想这是因为您在用户模型中使用了
receivesBroadcastNotificationsOn()
自定义了通知通道,并且您正在
App.User.*
上收听事件,而不是
User.*

因此,只需从用户模型中删除
receivesBroadcastNotificationsOn()
,然后再次检查


希望这有帮助

您能在GitHub上共享整个代码吗?当然,如果它只是一个演示,而不是一个现场项目。对不起,它太大了,而且还有一些秘密部分。。。。。你想知道什么?没关系,我会用你提供的代码来回答这个问题。既然你使用的是私人频道,你能粘贴routes/channels.php吗?是的!我编辑了我的帖子抱歉,但我的事件类中仍然有一个错误:/它不允许我使用私人频道。。。。对不起,我没听清楚。具体问题是什么?CreateTicketNotify工作正常,但CreateTicketEvent nor:/请在编辑的帖子中查看CreateTicketEvent我的路线:路线::获取('/event',function(){$ticket=App\Models\ticket\ticket::find(240);事件(新的App\Events\CreateTicketEvent($ticket));返回“事件已发送!”;);这里我的app.js更新了:window.Echo.private('ticket.0')。listen('CreateTicketEvent',(e)=>{//toastr.info('has created new ticket!!','info');console.log(e)});
Broadcast::channel('App.User.{id}', function ($user, $id) {
    return (int) $user->id === (int) $id;
});

Broadcast::channel('ticket.{id}', function ($user, $id) {
    // $ticket = App\Models\Ticket\Ticket::find($ticketID);
    // return $ticket->category->users->has(Auth::id());
    // if(Auth::id()==1)        
    // if($id == 0)
    //  return false;
    // else
        return true;
    $ticket = App\Models\Ticket\Ticket::find($id);

    if (in_array(Auth::id(), $ticket->category->users->pluck('id')->toArray()) )
        return true;
    else
        return false;

    // return (int) $user->id !== (int) App\Models\Ticket\Ticket::find($ticketID)->user->id;
    // else
    //  return false;
});
<?php

namespace App;

use Illuminate\Notifications\Notifiable;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Spatie\Permission\Traits\HasRoles;
use Spatie\MediaLibrary\HasMedia\HasMediaTrait;
use Spatie\MediaLibrary\HasMedia\Interfaces\HasMedia;
use Auth;

class User extends Authenticatable implements HasMedia
{
    use Notifiable, HasRoles, HasMediaTrait;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'lastname', 'firstname', 'email', 'password', 'active',
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];




    public function tickets()
    {
        return $this->hasMany('App\Models\Ticket\Ticket');
    } 

    public function receivesBroadcastNotificationsOn()
    {
        return 'users.'.$this->id;
    }
}
<?php

namespace App\Events;

use App\Events\Event;

use Illuminate\Broadcasting\Channel;
use Illuminate\Queue\SerializesModels;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Broadcasting\PresenceChannel;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use Illuminate\Contracts\Queue\ShouldQueue;


use App\Models\Ticket\Ticket;

class CreateTicketEvent implements ShouldQueue, ShouldBroadcast 
{
    use Dispatchable, InteractsWithSockets, SerializesModels;

    public $ticket;

    /**
     * Create a new event instance.
     *
     * @return void
     */
    public function __construct(Ticket $ticket)
    {
        $this->ticket = $ticket;
        //  $this->ticket = array(
        //     'power'=> '10'
        // );
    }

    /**
     * Get the channels the event should broadcast on.
     *
     * @return Channel|array
     */
    public function broadcastOn()
    {        
        return new PrivateChannel('ticket.' . $this->ticket->id);
        // return ['ticket'];
    }

    // public function broadcastWith()
    // {
    //     return array('subject' => $this->ticket->subject);
    // }
}
[03:31:28] - 8bnJMpO-fFVs9LSyAAAE joined channel: ticket
[03:31:28] - 8bnJMpO-fFVs9LSyAAAE authenticated for: private-App.User.2
[03:31:28] - 8bnJMpO-fFVs9LSyAAAE joined channel: private-App.User.2
Channel: private-users.2
Event: Illuminate\Notifications\Events\BroadcastNotificationCreated
CHANNEL private-users.2
window.Echo.private('App.User.' + window.Laravel.user.id)               
    .notification((notification) => {
        console.log(notification);
        // toastr.info(' has created new ticket !! ', 'info');        
    });