Skip to content

Commit 0ed2b6b

Browse files
committed
Part 41 - Added Comments Resource and Store(). Resolved #1
1 parent 0620383 commit 0ed2b6b

File tree

6 files changed

+210
-2
lines changed

6 files changed

+210
-2
lines changed

app/Comment.php

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace App;
4+
5+
use Illuminate\Database\Eloquent\Model;
6+
7+
class Comment extends Model
8+
{
9+
public function post()
10+
{
11+
return $this->belongsTo('App\Post');
12+
}
13+
}
+108
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
<?php
2+
3+
namespace App\Http\Controllers;
4+
5+
use Illuminate\Http\Request;
6+
7+
use App\Http\Requests;
8+
use App\Comment;
9+
use App\Post;
10+
use Session;
11+
12+
class CommentsController extends Controller
13+
{
14+
/**
15+
* Display a listing of the resource.
16+
*
17+
* @return \Illuminate\Http\Response
18+
*/
19+
public function index()
20+
{
21+
//
22+
}
23+
24+
/**
25+
* Show the form for creating a new resource.
26+
*
27+
* @return \Illuminate\Http\Response
28+
*/
29+
public function create()
30+
{
31+
//
32+
}
33+
34+
/**
35+
* Store a newly created resource in storage.
36+
*
37+
* @param \Illuminate\Http\Request $request
38+
* @return \Illuminate\Http\Response
39+
*/
40+
public function store(Request $request, $post_id)
41+
{
42+
$this->validate($request, array(
43+
'name' => 'required|max:255',
44+
'email' => 'required|email|max:255',
45+
'comment' => 'required|min:5|max:2000'
46+
));
47+
48+
$post = Post::find($post_id);
49+
50+
$comment = new Comment();
51+
$comment->name = $request->name;
52+
$comment->email = $request->email;
53+
$comment->comment = $request->comment;
54+
$comment->approved = true;
55+
$comment->post()->associate($post);
56+
57+
$comment->save();
58+
59+
Session::flash('success', 'Comment was added');
60+
61+
return redirect()->route('blog.single', [$post->slug]);
62+
}
63+
64+
/**
65+
* Display the specified resource.
66+
*
67+
* @param int $id
68+
* @return \Illuminate\Http\Response
69+
*/
70+
public function show($id)
71+
{
72+
//
73+
}
74+
75+
/**
76+
* Show the form for editing the specified resource.
77+
*
78+
* @param int $id
79+
* @return \Illuminate\Http\Response
80+
*/
81+
public function edit($id)
82+
{
83+
//
84+
}
85+
86+
/**
87+
* Update the specified resource in storage.
88+
*
89+
* @param \Illuminate\Http\Request $request
90+
* @param int $id
91+
* @return \Illuminate\Http\Response
92+
*/
93+
public function update(Request $request, $id)
94+
{
95+
//
96+
}
97+
98+
/**
99+
* Remove the specified resource from storage.
100+
*
101+
* @param int $id
102+
* @return \Illuminate\Http\Response
103+
*/
104+
public function destroy($id)
105+
{
106+
//
107+
}
108+
}

app/Http/routes.php

+3
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@
4141
// Categories
4242
Route::resource('categories', 'CategoryController', ['except' => ['create']]);
4343
Route::resource('tags', 'TagController', ['except' => ['create']]);
44+
45+
// Comments
46+
Route::post('comments/{post_id}', ['uses' => 'CommentsController@store', 'as' => 'comments.store']);
4447

4548

4649
Route::get('blog/{slug}', ['as' => 'blog.single', 'uses' => 'BlogController@getSingle'])->where('slug', '[\w\d\-\_]+');

app/Post.php

+5
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,9 @@ public function tags()
1515
{
1616
return $this->belongsToMany('App\Tag');
1717
}
18+
19+
public function comments()
20+
{
21+
return $this->hasMany('App\Comment');
22+
}
1823
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
use Illuminate\Database\Schema\Blueprint;
4+
use Illuminate\Database\Migrations\Migration;
5+
6+
class CreateCommentsTable extends Migration
7+
{
8+
/**
9+
* Run the migrations.
10+
*
11+
* @return void
12+
*/
13+
public function up()
14+
{
15+
Schema::create('comments', function (Blueprint $table) {
16+
$table->increments('id');
17+
$table->string('name');
18+
$table->string('email');
19+
$table->text('comment');
20+
$table->boolean('approved');
21+
$table->integer('post_id')->unsigned();
22+
$table->timestamps();
23+
});
24+
25+
Schema::table('comments', function ($table){
26+
$table->foreign('post_id')->references('id')->on('posts')->onDelete('cascade');
27+
});
28+
}
29+
30+
/**
31+
* Reverse the migrations.
32+
*
33+
* @return void
34+
*/
35+
public function down()
36+
{
37+
Schema::dropForeign(['post_id']);
38+
Schema::drop('comments');
39+
40+
}
41+
}

resources/views/blog/single.blade.php

+40-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
@extends('main')
2-
3-
@section('title', "| $post->title")
2+
<?php $titleTag = htmlspecialchars($post->title); ?>
3+
@section('title', "| $titleTag")
44

55
@section('content')
66

@@ -13,4 +13,42 @@
1313
</div>
1414
</div>
1515

16+
<div class="row">
17+
<div class="col-md-8 col-md-offset-2">
18+
@foreach($post->comments as $comment)
19+
<div class="comment">
20+
<p><strong>Name:</strong> {{ $comment->name }}</p>
21+
<p><strong>Comment:</strong><br/>{{ $comment->comment }}</p><br>
22+
</div>
23+
@endforeach
24+
</div>
25+
</div>
26+
27+
<div class="row">
28+
<div id="comment-form" class="col-md-8 col-md-offset-2" style="margin-top: 50px;">
29+
{{ Form::open(['route' => ['comments.store', $post->id], 'method' => 'POST']) }}
30+
31+
<div class="row">
32+
<div class="col-md-6">
33+
{{ Form::label('name', "Name:") }}
34+
{{ Form::text('name', null, ['class' => 'form-control']) }}
35+
</div>
36+
37+
<div class="col-md-6">
38+
{{ Form::label('email', 'Email:') }}
39+
{{ Form::text('email', null, ['class' => 'form-control']) }}
40+
</div>
41+
42+
<div class="col-md-12">
43+
{{ Form::label('comment', "Comment:") }}
44+
{{ Form::textarea('comment', null, ['class' => 'form-control', 'rows' => '5']) }}
45+
46+
{{ Form::submit('Add Comment', ['class' => 'btn btn-success btn-block', 'style' => 'margin-top:15px;']) }}
47+
</div>
48+
</div>
49+
50+
{{ Form::close() }}
51+
</div>
52+
</div>
53+
1654
@endsection

0 commit comments

Comments
 (0)