添加朋友时,它会改变我的身份/按钮,但不会改变另一方

时间:2017-11-12 07:15:38

标签: php laravel vue.js pusher

当我添加一个朋友时,它改变状态/按钮在我的身边而不是另一边。 我正在使用Pusher,Laravel和Vue.js。

我的问题是,如果我发送好友请求,它将发送给其他用户并通知我但不通知给另一方。我不知道我做错了什么。

这是我的频道代码。

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

这是我的NewFriendRequest.php代码

<?php

   namespace App\Notifications;

   use Illuminate\Bus\Queueable;
  use Illuminate\Notifications\Notification;
 use Illuminate\Contracts\Queue\ShouldQueue;
 use Illuminate\Notifications\Messages\MailMessage;

class NewFriendRequest extends Notification implements ShouldQueue
{
use Queueable;

public $user;

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

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

/**
 * Get the mail representation of the notification.
 *
 * @param  mixed  $notifiable
 * @return \Illuminate\Notifications\Messages\MailMessage
 */
public function toMail($notifiable)
{
    return (new MailMessage)
                ->line('You received a new friend request from ' . $this->user->name)
                ->action('View Profile', route('/profile', ['slug' => $this->user->slug]))
                ->line('Thank you for using our Tictac!');
}

/**
 * Get the array representation of the notification.
 *
 * @param  mixed  $notifiable
 * @return array
 */
public function toArray($notifiable)
{
    return [
        'name' => $this->user->name,
        'message' => $this->user->name . ' sent you friend request.'
     ];
}
}

这是我的notification.vue代码

<script>
  export default {
        mounted() {
              this.listen()
        },
        props: ['id'],
        methods: {
              listen() {

                    Echo.private('App.User.' + this.id)
                        .notification( (notification) => {
                              noty({
                                      type: 'success',
                                      layout: 'bottomLeft',
                                      text: notification.name + notification.message,
                                      timeout: 5000
                              }).show()

                              document.getElementById("noty_audio").play()
                        })




              }
        }
  }

这是我可以用来向朋友发送请求的Example.vue代码        其他用户。

  add_friend(){
      this.loading = true
        axios.get('http://localhost/Tictac/add_friend/' + 
    this.profile_user_id )
        .then ((r) => {
          if(r.data == 1)
          this.status = 'waiting'
        new Noty({
          type: 'success',
          layout: 'bottomLeft',
          text: 'Friend request sent .',
          timeout: 5000

          }).show()

          this.loading = false
        })

      },

我的bootstrap.js文件

   import Echo from 'laravel-echo'

   window.Pusher = require('pusher-js');

   window.Echo = new Echo({

 authEndpoint: 'http://localhost/Tictac/broadcasting/auth',
 broadcaster: 'pusher',
 key: '58f16da7e24fa9505db8',
 cluster: 'eu',
 encypted: true



  });

  Pusher.log = function(message){
  window.console.log(message)
  }

App.js

require('./bootstrap');

 window.Vue = require('vue');
Vue.component('example', require('./components/Example.vue'));
Vue.component('notification', require('./components/Notification.vue'));

const app = new Vue({
el: '#app'
});

0 个答案:

没有答案