This repository was archived by the owner on Aug 1, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPostController.php
More file actions
78 lines (67 loc) · 2.53 KB
/
PostController.php
File metadata and controls
78 lines (67 loc) · 2.53 KB
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use Illuminate\Routing\Controller;
use Illuminate\Support\Facades\Auth;
use App\Post;
use App\Comment;
class PostController extends Controller
{
//
public function showIndividualPost($id){
if (!Auth::check() && Post::where('id', $id)->value('is_login') == 1) {
return view('login_required')->withErrors('本主题需要登录后查看');
}
return view('post/show')->withPost(Post::with('hasManyComments','tagged')->find($id));
}
public function getReply(Request $request){
if ($user = $request->user()) {
if (!$request->has('content')) {
return redirect()->back()->withInput()->withErrors('评论为空!');
}
if ($comment = new Comment()) {
$comment->user_id = $user->id;
$comment->content = $request->content;
$comment->post_id = $request->post_id;
if (!$comment->save()) {
return redirect()->back()->withInput()->withErrors('评论发表失败!');
}
$post = Post::where('id', $request->post_id)->first();
$post->last_user = $user->id;
if (!$post->save()) {
return redirect()->back()->withInput()->withErrors('更新状态失败!');
}
return redirect()->back();
} else {
return view('login_required')->withErrors('回复需要登录');
}
}
}
public function createSite(){
return view('post/create');
}
public function createNewPost(Request $request)
{
if ($user = $request->user()){
if (!$request->has('content')){
return redirect()->back()->withInput()->withErrors('内容为空!');
}
if ($post = new Post()){
$post->user_id = $user->id;
$post->content = $request->content;
$post->title = $request->title;
$post->save(); // tags need post id !
$post->tag($request->tags);
if ($post->save()) {
return redirect()->back();
} else {
$post->delete(); // 防止错误
}
}
} else {
return view('login_required')->withErrors('发表需要登录');
}
return redirect()->back()->withInput()->withErrors('帖子发表失败!');
}
}