-
Notifications
You must be signed in to change notification settings - Fork 2
/
CommentController.php
62 lines (52 loc) · 1.68 KB
/
CommentController.php
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
<?php
namespace Infogue\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Lang;
use Illuminate\Support\Facades\Validator;
use Infogue\Article;
use Infogue\Comment;
use Infogue\Http\Requests;
class CommentController extends Controller
{
/*
|--------------------------------------------------------------------------
| Comment Controller
|--------------------------------------------------------------------------
|
| This controller is responsible for handling save request and store
| comment into storage.
|
*/
/**
* Store a newly comment in storage.
*
* @param \Illuminate\Http\Request
* @param $slug
* @return \Illuminate\Http\Response
*/
public function store(Request $request, $slug)
{
$rules = [
'comment' => 'required|max:2000',
];
$validator = Validator::make($request->all(), $rules);
if ($validator->fails()) {
$this->throwValidationException(
$request, $validator
);
}
$article = Article::whereSlug($slug)->firstOrFail();
$comment = new Comment();
$comment->contributor_id = Auth::user()->id;
$comment->article_id = $article->id;
$comment->comment = $request->input('comment');
if ($comment->save()) {
return redirect(route('article.show', [$slug]) . '#form-comment')->with([
'status' => 'success',
'message' => Lang::get('alert.comment.send')
]);
}
return redirect()->back()->withErrors(['error' => Lang::get('alert.error.database')]);
}
}