1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Bantenprov\VueBlog\Controllers; |
4
|
|
|
|
5
|
|
|
use App\Http\Controllers\Controller; |
6
|
|
|
use Illuminate\Support\Facades\Auth; |
7
|
|
|
use Illuminate\Http\Request; |
8
|
|
|
use Bantenprov\VueBlog\Models\Blog; |
9
|
|
|
use Bantenprov\VueBlog\Requests\StoreBlogPost; |
10
|
|
|
use Bantenprov\VueBlog\Requests\UpdateBlogPost; |
11
|
|
|
|
12
|
|
|
class BlogController extends Controller |
13
|
|
|
{ |
14
|
|
|
public function index() |
15
|
|
|
{ |
16
|
|
|
$articles = Blog::with('user')->latest('updated_at')->whereNull('deleted_at')->get(); |
17
|
|
|
$current_user = Auth::user(); |
18
|
|
|
return view('view::index', compact('articles', 'current_user')); |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
public function create() |
22
|
|
|
{ |
23
|
|
|
$tags = NULL; |
24
|
|
|
// return response()->json([ |
|
|
|
|
25
|
|
|
// 'tags' => $tags |
|
|
|
|
26
|
|
|
// ]); |
27
|
|
|
return view('view::create', compact('tags')); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
public function store(StoreBlogPost $request) |
31
|
|
|
{ |
32
|
|
|
$request['slug'] = str_slug($request->title, '-'); |
33
|
|
|
$post = new Blog(); |
34
|
|
|
$post->title = $request->title; |
35
|
|
|
$post->content = $request->content; |
36
|
|
|
$post->excerpt = $request->excerpt; |
37
|
|
|
$post->slug = str_slug($request->title, '-'); |
38
|
|
|
$user = Auth::user(); |
39
|
|
|
$user->posts()->save($post); |
|
|
|
|
40
|
|
|
|
41
|
|
|
// return response()->json([ |
|
|
|
|
42
|
|
|
// 'title' => 'Success', |
|
|
|
|
43
|
|
|
// 'type' => 'success', |
|
|
|
|
44
|
|
|
// 'message' => 'Your article has been created.' |
|
|
|
|
45
|
|
|
// ]); |
46
|
|
|
\Session::flash('flash_message', 'Your article has been created'); |
47
|
|
|
return response()->json($user); |
48
|
|
|
//return redirect()->route('blog.index'); |
|
|
|
|
49
|
|
|
} |
50
|
|
|
|
51
|
|
View Code Duplication |
public function show(Blog $blog) |
|
|
|
|
52
|
|
|
{ |
53
|
|
|
$article = $blog; |
54
|
|
|
$current_user = Auth::user(); |
55
|
|
|
$id_blog = $article->id; |
56
|
|
|
// return response()->json([ |
|
|
|
|
57
|
|
|
// 'articles' => $article, |
|
|
|
|
58
|
|
|
// 'current_user' => $current_user |
|
|
|
|
59
|
|
|
// ]); |
60
|
|
|
return view('view::show', compact('article', 'current_user', 'id_blog')); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
View Code Duplication |
public function edit($id) |
|
|
|
|
64
|
|
|
{ |
65
|
|
|
$tags = NULL; |
66
|
|
|
$article = Blog::findOrFail($id); |
67
|
|
|
$id_blog = $id; |
68
|
|
|
// return response()->json([ |
|
|
|
|
69
|
|
|
// 'articles' => $article, |
|
|
|
|
70
|
|
|
// 'tags' => $tags |
|
|
|
|
71
|
|
|
// ]); |
72
|
|
|
return view('view::edit', compact('article', 'tags', 'id_blog')); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
public function update(UpdateBlogPost $request, $id) |
76
|
|
|
{ |
77
|
|
|
$article = Blog::findOrFail($id); |
78
|
|
|
$article->update($request->all()); |
79
|
|
|
$this->syncTags($article, $request->input('tag_list')); |
80
|
|
|
$article->save(); |
81
|
|
|
|
82
|
|
|
// return response()->json([ |
|
|
|
|
83
|
|
|
// 'title' => 'Success', |
|
|
|
|
84
|
|
|
// 'type' => 'success', |
|
|
|
|
85
|
|
|
// 'message' => 'Your article has been updated.', |
|
|
|
|
86
|
|
|
// 'id' => $id |
|
|
|
|
87
|
|
|
// ]); |
88
|
|
|
return response()->json($article); |
89
|
|
|
//return redirect()->route('blog.show', $id); |
|
|
|
|
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
public function destroy($id) |
93
|
|
|
{ |
94
|
|
|
// Blog::find($id)->delete(); |
|
|
|
|
95
|
|
|
// return response()->json([ |
|
|
|
|
96
|
|
|
// 'title' => 'Error', |
|
|
|
|
97
|
|
|
// 'type' => 'error', |
|
|
|
|
98
|
|
|
// 'message' => 'Data deleted successfully' |
|
|
|
|
99
|
|
|
// ]); |
100
|
|
|
$hapus = Blog::destroy($id); |
101
|
|
|
// return redirect()->route('blog.index'); |
|
|
|
|
102
|
|
|
return response()->json($hapus); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
private function syncTags(Blog $article, $tags = []) |
106
|
|
|
{ |
107
|
|
|
if (empty($tags)) { |
108
|
|
|
return; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
$article->tags()->sync($tags); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
private function createPost(StoreBlogPost $request) |
|
|
|
|
115
|
|
|
{ |
116
|
|
|
$request['slug'] = str_slug($request->title, '-'); |
117
|
|
|
$article = Auth::user() |
|
|
|
|
118
|
|
|
->posts() |
119
|
|
|
->save(new Blog($request->all())); |
120
|
|
|
//return response()->json($article); |
|
|
|
|
121
|
|
|
return $article; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
public function getData(Request $request) |
125
|
|
|
{ |
126
|
|
|
$cari = $request->get('cari'); |
127
|
|
|
$id = $request->get('id'); |
128
|
|
|
$current_user = Auth::user(); |
129
|
|
|
if($cari){ |
130
|
|
|
if($id){ |
131
|
|
|
$articles = Blog::with('user') |
132
|
|
|
->latest('updated_at') |
133
|
|
|
->where('id', $id) |
134
|
|
|
->where('title', 'LIKE','%'.$cari.'%') |
135
|
|
|
->whereNull('deleted_at') |
136
|
|
|
->paginate(5) |
137
|
|
|
->appends($request->only('cari')); |
138
|
|
|
}else { |
139
|
|
|
$articles = Blog::with('user') |
140
|
|
|
->latest('updated_at') |
141
|
|
|
->where('title', 'LIKE','%'.$cari.'%') |
142
|
|
|
->whereNull('deleted_at') |
143
|
|
|
->paginate(5) |
144
|
|
|
->appends($request->only('cari')); |
145
|
|
|
} |
146
|
|
|
} else { |
147
|
|
|
if($id){ |
148
|
|
|
$articles = Blog::with('user') |
149
|
|
|
->latest('updated_at') |
150
|
|
|
->where('id', $id) |
151
|
|
|
->whereNull('deleted_at') |
152
|
|
|
->paginate(5); |
153
|
|
|
}else { |
154
|
|
|
$articles = Blog::with('user') |
155
|
|
|
->latest('updated_at') |
156
|
|
|
->whereNull('deleted_at') |
157
|
|
|
->paginate(5); |
158
|
|
|
} |
159
|
|
|
} |
160
|
|
|
return response()->json([ |
161
|
|
|
'articles' => $articles, |
162
|
|
|
'current_user' => $current_user |
163
|
|
|
]); |
164
|
|
|
} |
165
|
|
|
} |
166
|
|
|
|
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.