Skip to content

Commit 1a7ef87

Browse files
committed
Part 3 - Updated Channel Changes
1 parent 715ed23 commit 1a7ef87

File tree

3 files changed

+66
-2
lines changed

3 files changed

+66
-2
lines changed

app/Events/NewComment.php

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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+
}

app/Http/Controllers/CommentController.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use App\Post;
77
use App\Comment;
88
use Auth;
9+
use App\Events\NewComment;
910

1011
class 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
}

resources/views/posts/show.blade.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,13 @@
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 &gt;&gt;</a>
25+
</div>
2326

2427

2528
<div class="media" style="margin-top:20px;" v-for="comment in comments">
@@ -53,6 +56,7 @@
5356
},
5457
mounted() {
5558
this.getComments();
59+
this.listen();
5660
},
5761
methods: {
5862
getComments() {
@@ -76,6 +80,12 @@
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
})

0 commit comments

Comments
 (0)