Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class BroadcastServiceProvider extends ServiceProvider
- {
- /**
- * Bootstrap any application services.
- *
- * @return void
- */
- public function boot()
- {
- Broadcast::routes();
- require base_path('routes/channels.php');
- /*
- * Authenticate the user's personal channel..
- */
- Broadcast::channel('App.user.{userId}', function ($user, $userId) {
- return (int) $user->id === (int) $userId;
- });
- }
- }
- public function addFriend($userRequested)
- {
- $resp = Auth::user()->addFriend($userRequested);
- User::find($userRequested)->notify(new \App\Notifications\NewFriendRequest(Auth::user()) );
- return $resp;
- }
- public function acceptFriend($requester)
- {
- $resp = Auth::user()->acceptFriend($requester);
- User::find($requester)->notify(new \App\Notifications\FriendRequestAccepted(Auth::user()));
- return $resp;
- }
- <template>
- <div>
- </div>
- </template>
- <script>
- export default {
- mounted() {
- this.listen()
- },
- props: ['id'],
- methods: {
- listen() {
- window.Echo.private('App.user.' + this.id)
- .notification( (notification) => {
- alert('new notification')
- console.log(notification)
- })
- }
- }
- }
- </script>
- <?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 receieved a new friend request from.' . $this->user->username)
- ->action('View their profile', route('userProfile', ['username' => $this->user->username]))
- ->line('Thank you for using our service!')
- ->line('www.Myezwin.com');
- }
- /**
- * Get the array representation of the notification.
- *
- * @param mixed $notifiable
- * @return array
- */
- public function toArray($notifiable)
- {
- return [
- 'username' => $this->user->username,
- 'message' => 'sent you a friend request.'
- ];
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement