1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Chriscreates\Blog\Controllers; |
4
|
|
|
|
5
|
|
|
use Chriscreates\Blog\Category; |
6
|
|
|
use Chriscreates\Blog\Post; |
7
|
|
|
use Chriscreates\Blog\Requests\ValidatePostRequest; |
8
|
|
|
use Chriscreates\Blog\Tag; |
9
|
|
|
use Illuminate\Http\JsonResponse; |
10
|
|
|
use Illuminate\Http\Request; |
11
|
|
|
use Illuminate\Support\Facades\Storage; |
12
|
|
|
|
13
|
|
|
class PostController extends Controller |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* Instantiate a new controller instance. |
17
|
|
|
* |
18
|
|
|
* @return void |
|
|
|
|
19
|
|
|
*/ |
20
|
|
|
public function __construct() |
21
|
|
|
{ |
22
|
|
|
$this->middleware('auth'); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Display a listing of the resource. |
27
|
|
|
* |
28
|
|
|
* @return \Illuminate\Http\Response |
29
|
|
|
*/ |
30
|
|
|
public function index() |
31
|
|
|
{ |
32
|
|
|
$posts = Post::withoutGlobalScope('tags') |
33
|
|
|
->orderBy('created_at', 'desc') |
34
|
|
|
->paginate(10); |
35
|
|
|
|
36
|
|
|
return view('blog.admin.posts.posts', compact('posts')); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Show the form for creating a new resource. |
41
|
|
|
* |
42
|
|
|
* @return \Illuminate\Http\Response |
43
|
|
|
*/ |
44
|
|
View Code Duplication |
public function create() |
|
|
|
|
45
|
|
|
{ |
46
|
|
|
$post = new Post; |
47
|
|
|
|
48
|
|
|
return view('blog.admin.posts.post', [ |
49
|
|
|
'post' => $post, |
50
|
|
|
'categories' => Category::all(), |
51
|
|
|
'tags' => Tag::all(), |
52
|
|
|
]); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Store a newly created resource in storage. |
57
|
|
|
* |
58
|
|
|
* @param \Chriscreates\Blog\Requests\ValidatePostRequest $request |
59
|
|
|
* @return \Illuminate\Http\JsonResponse |
60
|
|
|
*/ |
61
|
|
|
public function store(ValidatePostRequest $request) : JsonResponse |
62
|
|
|
{ |
63
|
|
|
$post = Post::create($request->only([ |
64
|
|
|
'title', |
65
|
|
|
'sub_title', |
66
|
|
|
'slug', |
67
|
|
|
'user_id', |
68
|
|
|
'excerpt', |
69
|
|
|
'content', |
70
|
|
|
'allow_comments', |
71
|
|
|
'allow_guest_comments', |
72
|
|
|
])); |
73
|
|
|
|
74
|
|
|
return response()->json($post); |
|
|
|
|
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Show the form for editing the specified resource. |
79
|
|
|
* |
80
|
|
|
* @param \Chriscreates\Blog\Post $post |
81
|
|
|
* @return \Illuminate\Http\Response |
82
|
|
|
*/ |
83
|
|
View Code Duplication |
public function edit(Post $post) |
|
|
|
|
84
|
|
|
{ |
85
|
|
|
$post->load('category', 'tags'); |
86
|
|
|
|
87
|
|
|
return view('blog.admin.posts.post', [ |
88
|
|
|
'post' => $post, |
89
|
|
|
'categories' => Category::all(), |
90
|
|
|
'tags' => Tag::all(), |
91
|
|
|
]); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Update the specified resource in storage. |
96
|
|
|
* |
97
|
|
|
* @param \Chriscreates\Blog\Requests\ValidatePostRequest $request |
98
|
|
|
* @param \Chriscreates\Blog\Post $post |
99
|
|
|
* @return \Illuminate\Http\JsonResponse |
100
|
|
|
*/ |
101
|
|
|
public function update(ValidatePostRequest $request, Post $post) : JsonResponse |
102
|
|
|
{ |
103
|
|
|
$post->update($request->only([ |
104
|
|
|
'title', |
105
|
|
|
'sub_title', |
106
|
|
|
'slug', |
107
|
|
|
'user_id', |
108
|
|
|
'excerpt', |
109
|
|
|
'content', |
110
|
|
|
'allow_comments', |
111
|
|
|
'allow_guest_comments', |
112
|
|
|
])); |
113
|
|
|
|
114
|
|
|
$post->refresh(); |
115
|
|
|
|
116
|
|
|
return response()->json($post); |
|
|
|
|
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Remove the specified resource from storage. |
121
|
|
|
* |
122
|
|
|
* @param \Chriscreates\Blog\Post $post |
123
|
|
|
* @return \Illuminate\Http\JsonResponse |
124
|
|
|
*/ |
125
|
|
|
public function destroy(Post $post) : JsonResponse |
126
|
|
|
{ |
127
|
|
|
$post->delete(); |
128
|
|
|
|
129
|
|
|
$post = new Post; |
130
|
|
|
|
131
|
|
|
return response()->json($post); |
|
|
|
|
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|
Adding a
@return
annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.