· 5 years ago · Aug 27, 2020, 09:50 AM
1// App\Jobs\NotifyNewPost.php
2
3<?php
4
5namespace App\Jobs;
6
7use App\CompanyPost;
8use App\Notifications\NewPost;
9use App\User;
10use Illuminate\Bus\Queueable;
11use Illuminate\Contracts\Queue\ShouldQueue;
12use Illuminate\Foundation\Bus\Dispatchable;
13use Illuminate\Queue\InteractsWithQueue;
14use Illuminate\Queue\SerializesModels;
15
16class NotifyNewPost implements ShouldQueue
17{
18 use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
19
20 /** @var \App\User */
21 public $user;
22
23 /** @var \App\CompanyPost */
24 public $post;
25
26 /**
27 * Create a new job instance.
28 *
29 * @param User $user
30 * @param CompanyPost $post
31 *
32 * @return void
33 */
34 public function __construct(User $user, CompanyPost $post)
35 {
36 $this->user = $user;
37 $this->post = $post;
38 }
39
40 /**
41 * Execute the job.
42 *
43 * @return void
44 */
45 public function handle()
46 {
47 $this->user->notify(new NewPost($this->post));
48 }
49}
50
51// App\Notification\NewPost.php
52
53<?php
54
55namespace App\Notifications;
56
57use App\CompanyPost;
58use Illuminate\Bus\Queueable;
59use Illuminate\Notifications\Messages\BroadcastMessage;
60use Illuminate\Notifications\Notification;
61
62class NewPost extends Notification
63{
64 use Queueable;
65
66 /** @var \App\CompanyPost */
67 public $post;
68
69
70 /**
71 * Create a new notification instance.
72 *
73 * @param CompanyPost $post
74 *
75 * @return void
76 */
77 public function __construct(CompanyPost $post)
78 {
79 $this->post = $post;
80 }
81
82 /**
83 * Get the notification's delivery channels.
84 *
85 * @param mixed $notifiable
86 * @return array
87 */
88 public function via($notifiable)
89 {
90 return ['database', 'broadcast'];
91 }
92
93 public function toDatabase($notifiable): array
94 {
95 return [
96 'message' => "Новый пост: {$this->post->title}",
97 'typeNotify' => 'new_post',
98 'url' => route('posts.show', ['post' => $this->post->id])
99 ];
100 }
101
102 public function toBroadcast($notifiable)
103 {
104 return new BroadcastMessage([
105 'message' => "Новый пост: {$this->post->title}",
106 'typeNotify' => 'new_post',
107 'url' => route('posts.show', ['post' => $this->post->id])
108 ]);
109 }
110
111 /**
112 * Get the array representation of the notification.
113 *
114 * @param mixed $notifiable
115 * @return array
116 */
117 public function toArray($notifiable)
118 {
119 return [
120 //
121 ];
122 }
123}
124
125// App\Obserbers\CompanyPostObserver
126<?php
127
128namespace App\Observers;
129
130use App\CompanyPost;
131use App\User;
132use App\Jobs\NotifyNewPost;
133
134class CompanyPostObserver
135{
136 /**
137 * Handle the company post "created" event.
138 *
139 * @param \App\CompanyPost $companyPost
140 * @return void
141 */
142 public function created(CompanyPost $companyPost)
143 {
144 $users = User::all();
145 foreach ($users as $user) {
146 NotifyNewPost::dispatch($user, $companyPost);
147 }
148 }
149}
150
151
152// resources/js/bootstrap.js
153window._ = require('lodash');
154try {
155 window.Popper = require('popper.js').default
156 window.$ = window.jQuery = require('jquery');
157 require('bootstrap')
158} catch (e) {
159
160}
161
162/**
163 * We'll load the axios HTTP library which allows us to easily issue requests
164 * to our Laravel back-end. This library automatically handles sending the
165 * CSRF token as a header based on the value of the "XSRF" token cookie.
166 */
167
168window.axios = require('axios');
169
170window.axios.defaults.headers.common['X-Requested-With'] = 'XMLHttpRequest';
171window.axios.defaults.baseURL = process.env.MIX_BASE_URL;
172window.axios.defaults.headers.common['Access-Control-Allow-Origin'] = '*';
173let token = document.head.querySelector('meta[name="csrf-token"]');
174
175if (token) {
176 window.axios.defaults.headers.common['X-CSRF-TOKEN'] = token.content;
177} else {
178 console.error('CSRF token not found: https://laravel.com/docs/csrf#csrf-x-csrf-token');
179}
180
181/**
182 * Echo exposes an expressive API for subscribing to channels and listening
183 * for events that are broadcast by Laravel. Echo and event broadcasting
184 * allows your team to easily build robust real-time web applications.
185 */
186
187import Echo from 'laravel-echo';
188
189window.Pusher = require('pusher-js');
190
191window.Echo = new Echo({
192 broadcaster: 'pusher',
193 key: "PUSHER_KEY",
194 cluster: "en",
195 forceTLS: false,
196});
197
198
199// In Vue component
200 mounted() {
201 Echo.private('App.User.' + this.userId)
202 .notification((notification) => {
203 console.log(notification)
204 this.unreadNotifications.push(notification);
205 });
206 },