This repository has been archived by the owner on Jul 5, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
snippets.txt
65 lines (54 loc) · 1.79 KB
/
snippets.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
For the post view
<a href="{{ route('comments.create', $post->id) }}">Add a comment</a>
Create for comment
/**
* @param \App\Post $post
*
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
*/
public function create(Post $post)
{
$comment = new Comment();
return view('comments.create', compact(['comment', 'post']));
}
Store for commentController
/**
* @param \Illuminate\Http\Request $request
* @param \App\Post $post
*
* @return \Illuminate\Http\RedirectResponse
*/
public function store(Request $request, Post $post)
{
/**
* Validate the input against our validation rules
*
* https://laravel.com/docs/5.5/validation#available-validation-rules
*
* We can also define our own validation rules.
*/
$this->validate($request, [
'user_name' => 'required',
'body' => 'required|max:255'
]);
/**
* If we're getting here we can go ahead and save the comment.
*
* We have the ability to mass assign the request variables to the model
* vars, but we'll do it the long way here: https://laravel.com/docs/5.5/eloquent#mass-assignment
*/
$comment = new Comment;
$comment->user_name = $request->user_name;
$comment->body = $request->body;
$comment->post_id = $post->id;
$comment->save();
/**
* Flash a success message and redirect back to the post.
*/
$request->session()->flash('message', 'Comment was successfully added!');
return redirect()->route('posts.show', $post->slug);
}
@if(count($post->comments) > 0)
<h3>Comments</h3>
@each('comments.show', $post->comments, 'comment')
@endif