File tree Expand file tree Collapse file tree 3 files changed +66
-2
lines changed Expand file tree Collapse file tree 3 files changed +66
-2
lines changed Original file line number Diff line number Diff line change 1+ <?php
2+
3+ namespace App \Events ;
4+
5+ use App \Comment ;
6+ use Illuminate \Broadcasting \Channel ;
7+ use Illuminate \Queue \SerializesModels ;
8+ use Illuminate \Broadcasting \PrivateChannel ;
9+ use Illuminate \Broadcasting \PresenceChannel ;
10+ use Illuminate \Foundation \Events \Dispatchable ;
11+ use Illuminate \Broadcasting \InteractsWithSockets ;
12+ use Illuminate \Contracts \Broadcasting \ShouldBroadcast ;
13+ use Illuminate \Contracts \Broadcasting \ShouldBroadcastNow ;
14+
15+ class NewComment implements ShouldBroadcastNow
16+ {
17+ use Dispatchable, InteractsWithSockets, SerializesModels;
18+
19+
20+ public $ comment ;
21+
22+ /**
23+ * Create a new event instance.
24+ *
25+ * @return void
26+ */
27+ public function __construct (Comment $ comment )
28+ {
29+ $ this ->comment = $ comment ;
30+ }
31+
32+ /**
33+ * Get the channels the event should broadcast on.
34+ *
35+ * @return \Illuminate\Broadcasting\Channel|array
36+ */
37+ public function broadcastOn ()
38+ {
39+ return new Channel ('post. ' .$ this ->comment ->post ->id );
40+ }
41+
42+ public function broadcastWith ()
43+ {
44+ return [
45+ 'body ' => $ this ->comment ->body ,
46+ 'created_at ' => $ this ->comment ->created_at ->toFormattedDateString (),
47+ 'user ' => [
48+ 'name ' => $ this ->comment ->user ->name ,
49+ 'avatar ' => 'http://lorempixel.com/50/50 '
50+ ]
51+ ];
52+ }
53+ }
Original file line number Diff line number Diff line change 66use App \Post ;
77use App \Comment ;
88use Auth ;
9+ use App \Events \NewComment ;
910
1011class CommentController extends Controller
1112{
@@ -22,7 +23,7 @@ public function store(Request $request, Post $post)
2223 ]);
2324
2425 $ comment = Comment::where ('id ' , $ comment ->id )->with ('user ' )->first ();
25-
26+ broadcast ( new NewComment ( $ comment ))-> toOthers ();
2627 return $ comment ->toJson ();
2728 }
2829}
Original file line number Diff line number Diff line change 1616 <hr />
1717
1818 <h3 >Comments:</h3 >
19- <div style =" margin-bottom :50px ;" >
19+ <div style =" margin-bottom :50px ;" v-if = " user " >
2020 <textarea class =" form-control" rows =" 3" name =" body" placeholder =" Leave a comment" v-model =" commentBox" ></textarea >
2121 <button class =" btn btn-success" style =" margin-top :10px " @click .prevent =" postComment" >Save Comment</button >
2222 </div >
23+ <div v-else >
24+ <h4 >You must be logged in to submit a comment!</h4 > <a href =" /login" >Login Now > ;> ; </a >
25+ </div >
2326
2427
2528 <div class =" media" style =" margin-top :20px ;" v-for =" comment in comments" >
5356 },
5457 mounted () {
5558 this .getComments ();
59+ this .listen ();
5660 },
5761 methods: {
5862 getComments () {
7680 .catch ((error ) => {
7781 console .log (error);
7882 })
83+ },
84+ listen () {
85+ Echo .channel (' post.' + this .post .id )
86+ .listen (' NewComment' , (comment ) => {
87+ this .comments .unshift (comment);
88+ })
7989 }
8090 }
8191 })
You can’t perform that action at this time.
0 commit comments