Skip to content

Commit 715ed23

Browse files
committed
Part 2 - Created Comments Model and API Endpoints
1 parent 03bdc1d commit 715ed23

File tree

7 files changed

+147
-8
lines changed

7 files changed

+147
-8
lines changed

app/Comment.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespace App;
4+
5+
use Illuminate\Database\Eloquent\Model;
6+
7+
class Comment extends Model
8+
{
9+
protected $fillable = [
10+
'body', 'user_id', 'post_id'
11+
];
12+
13+
public function post()
14+
{
15+
return $this->belongsTo('App\Post');
16+
}
17+
18+
public function user()
19+
{
20+
return $this->belongsTo('App\User');
21+
}
22+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace App\Http\Controllers;
4+
5+
use Illuminate\Http\Request;
6+
use App\Post;
7+
use App\Comment;
8+
use Auth;
9+
10+
class CommentController extends Controller
11+
{
12+
public function index(Post $post)
13+
{
14+
return response()->json($post->comments()->with('user')->latest()->get());
15+
}
16+
17+
public function store(Request $request, Post $post)
18+
{
19+
$comment = $post->comments()->create([
20+
'body' => $request->body,
21+
'user_id' => Auth::id()
22+
]);
23+
24+
$comment = Comment::where('id', $comment->id)->with('user')->first();
25+
26+
return $comment->toJson();
27+
}
28+
}

app/Post.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,9 @@ public function user()
1414
{
1515
return $this->belongsTo('App\User');
1616
}
17+
18+
public function comments()
19+
{
20+
return $this->hasMany('App\Comment');
21+
}
1722
}

app/User.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,9 @@ public function accounts() {
3434
public function posts() {
3535
return $this->hasMany('App\Post');
3636
}
37+
38+
public function comments()
39+
{
40+
return $this->hasMany('App\Comment');
41+
}
3742
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
use Illuminate\Support\Facades\Schema;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Database\Migrations\Migration;
6+
7+
class CreateCommentsTable extends Migration
8+
{
9+
/**
10+
* Run the migrations.
11+
*
12+
* @return void
13+
*/
14+
public function up()
15+
{
16+
Schema::create('comments', function (Blueprint $table) {
17+
$table->increments('id');
18+
$table->integer('user_id')->unsigned();
19+
$table->integer('post_id')->unsigned();
20+
$table->text('body');
21+
$table->timestamps();
22+
});
23+
}
24+
25+
/**
26+
* Reverse the migrations.
27+
*
28+
* @return void
29+
*/
30+
public function down()
31+
{
32+
Schema::dropIfExists('comments');
33+
}
34+
}

resources/views/posts/show.blade.php

Lines changed: 50 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,24 +17,68 @@
1717

1818
<h3>Comments:</h3>
1919
<div style="margin-bottom:50px;">
20-
<textarea class="form-control" rows="3" name="body" placeholder="Leave a comment"></textarea>
21-
<button class="btn btn-success" style="margin-top:10px">Save Comment</button>
20+
<textarea class="form-control" rows="3" name="body" placeholder="Leave a comment" v-model="commentBox"></textarea>
21+
<button class="btn btn-success" style="margin-top:10px" @click.prevent="postComment">Save Comment</button>
2222
</div>
2323

2424

25-
<div class="media" style="margin-top:20px;">
25+
<div class="media" style="margin-top:20px;" v-for="comment in comments">
2626
<div class="media-left">
2727
<a href="#">
2828
<img class="media-object" src="http://placeimg.com/80/80" alt="...">
2929
</a>
3030
</div>
3131
<div class="media-body">
32-
<h4 class="media-heading">John Doe said...</h4>
32+
<h4 class="media-heading">@{{comment.user.name}} said...</h4>
3333
<p>
34-
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
34+
@{{comment.body}}
3535
</p>
36-
<span style="color: #aaa;">on Dec 15, 2017</span>
36+
<span style="color: #aaa;">on @{{comment.created_at}}</span>
3737
</div>
3838
</div>
3939
</div>
4040
@endsection
41+
42+
43+
@section('scripts')
44+
<script>
45+
46+
const app = new Vue({
47+
el: '#app',
48+
data: {
49+
comments: {},
50+
commentBox: '',
51+
post: {!! $post->toJson() !!},
52+
user: {!! Auth::check() ? Auth::user()->toJson() : 'null' !!}
53+
},
54+
mounted() {
55+
this.getComments();
56+
},
57+
methods: {
58+
getComments() {
59+
axios.get('/api/posts/'+this.post.id+'/comments')
60+
.then((response) => {
61+
this.comments = response.data
62+
})
63+
.catch(function (error) {
64+
console.log(error);
65+
});
66+
},
67+
postComment() {
68+
axios.post('/api/posts/'+this.post.id+'/comment', {
69+
api_token: this.user.api_token,
70+
body: this.commentBox
71+
})
72+
.then((response) => {
73+
this.comments.unshift(response.data);
74+
this.commentBox = '';
75+
})
76+
.catch((error) => {
77+
console.log(error);
78+
})
79+
}
80+
}
81+
})
82+
83+
</script>
84+
@endsection

routes/api.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
| is assigned the "api" middleware group. Enjoy building your API!
1313
|
1414
*/
15+
Route::get('/posts/{post}/comments', 'CommentController@index');
1516

16-
Route::middleware('auth:api')->get('/user', function (Request $request) {
17-
return $request->user();
17+
Route::middleware('auth:api')->group(function () {
18+
Route::post('/posts/{post}/comment', 'CommentController@store');
1819
});

0 commit comments

Comments
 (0)