1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Http\Controllers\Backend; |
4
|
|
|
|
5
|
|
|
use Carbon\Carbon; |
6
|
|
|
use App\Models\Post; |
7
|
|
|
use Illuminate\Http\Request; |
8
|
|
|
use App\Http\Controllers\Controller; |
9
|
|
|
use App\Http\Resources\PostResource; |
10
|
|
|
use Lloople\Notificator\Notificator; |
11
|
|
|
use App\Http\Requests\PostFormRequest; |
12
|
|
|
use App\ViewModels\PostDetailViewModel; |
13
|
|
|
|
14
|
|
|
class PostsController extends Controller |
15
|
|
|
{ |
16
|
|
|
private $pagination = 15; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Display a listing of the resource. |
20
|
|
|
* |
21
|
|
|
* @param \Illuminate\Http\Request $request |
22
|
|
|
* @return \Illuminate\Http\Response |
23
|
|
|
*/ |
24
|
|
|
public function index(Request $request) |
|
|
|
|
25
|
|
|
{ |
26
|
|
|
return view('backend.posts.index'); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Show the form for creating a new resource. |
31
|
|
|
* |
32
|
|
|
* @return \Illuminate\Http\Response |
33
|
|
|
*/ |
34
|
|
|
public function create() |
35
|
|
|
{ |
36
|
|
|
return view('backend.posts.edit', ['view' => new PostDetailViewModel(new Post())]); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Store a newly created resource in storage. |
41
|
|
|
* |
42
|
|
|
* @param \App\Http\Requests\PostFormRequest $request |
43
|
|
|
* |
44
|
|
|
* @return \Illuminate\Http\RedirectResponse |
45
|
|
|
*/ |
46
|
|
View Code Duplication |
public function store(PostFormRequest $request) |
|
|
|
|
47
|
|
|
{ |
48
|
|
|
$post = new Post(); |
49
|
|
|
$post->title = $request->title; |
|
|
|
|
50
|
|
|
$post->slug = str_slug($request->title); |
|
|
|
|
51
|
|
|
$post->category_id = $request->category; |
|
|
|
|
52
|
|
|
$post->published_at = Carbon::createFromFormat('Y-m-d\TH:i', $request->published_at); |
|
|
|
|
53
|
|
|
$post->featured = $request->has('featured'); |
54
|
|
|
$post->visible = $request->has('visible'); |
|
|
|
|
55
|
|
|
$post->body = $request->body; |
|
|
|
|
56
|
|
|
|
57
|
|
|
$post->save(); |
58
|
|
|
|
59
|
|
|
$post->syncTags($request->tags); |
|
|
|
|
60
|
|
|
|
61
|
|
|
Notificator::success('Post created successfully'); |
62
|
|
|
|
63
|
|
|
return redirect()->route('backend.posts.edit', $post); |
|
|
|
|
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Show the form for editing the specified resource. |
68
|
|
|
* |
69
|
|
|
* @param \App\Models\Post $post |
70
|
|
|
* |
71
|
|
|
* @return void |
72
|
|
|
*/ |
73
|
|
|
public function edit(Post $post) |
74
|
|
|
{ |
75
|
|
|
return view('backend.posts.edit', ['view' => new PostDetailViewModel($post)]); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Update the specified resource in storage. |
80
|
|
|
* |
81
|
|
|
* @param \App\Http\Requests\PostFormRequest $request |
82
|
|
|
* @param Post $post |
83
|
|
|
* |
84
|
|
|
* @return \Illuminate\Http\RedirectResponse |
85
|
|
|
*/ |
86
|
|
View Code Duplication |
public function update(PostFormRequest $request, Post $post) |
|
|
|
|
87
|
|
|
{ |
88
|
|
|
$post->title = $request->title; |
|
|
|
|
89
|
|
|
$post->slug = str_slug($request->title); |
|
|
|
|
90
|
|
|
$post->category_id = $request->category; |
|
|
|
|
91
|
|
|
$post->published_at = Carbon::createFromFormat('Y-m-d\TH:i', $request->published_at); |
|
|
|
|
92
|
|
|
$post->featured = $request->has('featured'); |
93
|
|
|
$post->visible = $request->has('visible'); |
|
|
|
|
94
|
|
|
$post->body = $request->body; |
|
|
|
|
95
|
|
|
|
96
|
|
|
$post->save(); |
97
|
|
|
|
98
|
|
|
$post->syncTags($request->tags); |
|
|
|
|
99
|
|
|
|
100
|
|
|
Notificator::success('Post edited successfully'); |
101
|
|
|
|
102
|
|
|
return redirect()->route('backend.posts.edit', $post); |
|
|
|
|
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Remove the specified resource from storage. |
107
|
|
|
* |
108
|
|
|
* @param \Illuminate\Http\Request $request |
109
|
|
|
* @param Post $post |
110
|
|
|
* |
111
|
|
|
* @return array |
112
|
|
|
* @throws \Exception |
113
|
|
|
*/ |
114
|
|
|
public function destroy(Request $request, Post $post) |
115
|
|
|
{ |
116
|
|
|
$post->tags()->sync([]); |
117
|
|
|
|
118
|
|
|
$post->delete(); |
119
|
|
|
|
120
|
|
|
if ($request->ajax()) { |
121
|
|
|
return [ |
122
|
|
|
'result' => true, |
123
|
|
|
'message' => 'Post deleted successfully.', |
124
|
|
|
]; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
Notificator::success('Post deleted successfully.'); |
128
|
|
|
|
129
|
|
|
return redirect()->route('backend.posts.index'); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
View Code Duplication |
public function resource(Request $request) |
|
|
|
|
133
|
|
|
{ |
134
|
|
|
$posts = Post::with('tags'); |
135
|
|
|
|
136
|
|
|
if ($request->has('q')) { |
137
|
|
|
$posts->searchLike($request->q); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
return PostResource::collection($posts->orderBy('published_at', 'DESC')->paginate($this->pagination)); |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.