@@ -116,10 +116,7 @@ public function draft(Request $request)
116
116
*/
117
117
public function show (Request $ request , $ slug )
118
118
{
119
-
120
- $ titleArray = explode ('- ' , $ slug );
121
- $ id = end ($ titleArray );
122
- $ blog = Blog::find ($ id );
119
+ $ blog = Blog::where ("slug " , $ slug )->first ();
123
120
if ($ blog ) {
124
121
if ($ blog ->status == "posted " ) {
125
122
// $shareBlog = ShareFacade::page(
@@ -136,11 +133,11 @@ public function show(Request $request, $slug)
136
133
// dd($blog->body());
137
134
138
135
139
- $ existView = BlogView::where ([['ip_address ' , "= " , $ request ->ip ()], ["blog_id " , "= " , $ id ]])->count ();
136
+ $ existView = BlogView::where ([['ip_address ' , "= " , $ request ->ip ()], ["blog_id " , "= " , $ blog -> id ]])->count ();
140
137
if ($ existView < 1 ) {
141
138
$ newView = new BlogView ();
142
139
$ newView ->ip_address = $ request ->ip ();
143
- $ newView ->blog_id = $ id ;
140
+ $ newView ->blog_id = $ blog -> id ;
144
141
$ newView ->save ();
145
142
}
146
143
$ related = Blog::where ("status " , "= " , "posted " )->with (['user ' , 'tags ' , 'bloglikes ' , 'blogviews ' ])->whereHas ('tags ' , function ($ query ) use ($ blog ) {
@@ -155,132 +152,25 @@ public function show(Request $request, $slug)
155
152
}
156
153
return abort (404 );;
157
154
}
158
- /**
159
- * Show the form for editing the specified resource.
160
- *
161
- * @param \App\Models\Blog $blog
162
- * @return \Illuminate\Http\Response
163
- */
164
-
165
- /**
166
- * Update the specified resource in storage.
167
- *
168
- * @param \App\Http\Requests\UpdateBlogRequest $request
169
- * @param \App\Models\Blog $blog
170
- * @return \Illuminate\Http\Response
171
- */
172
- public function store (StoreBlogRequest $ request )
173
- {
174
- // dd($request);
175
- // {{ Str::slug($blog->title(), '-') }}-{{ $blog->id() }}
176
- return $ request ;
177
- }
178
- public function post (Request $ request )
155
+ public function edit ($ slug )
179
156
{
180
- dd ($ request ->all ());
181
- $ blogId = $ request ->get ('blog_id ' );
182
- $ blogTitle = $ request ->get ('title ' );
183
- $ blogDescription = $ request ->get ('description ' );
184
- $ tagNames = json_decode ($ request ->get ('tags ' ));
185
- if ($ blogId != NULL ) {
186
- $ blog = Blog::find ($ blogId );
187
- $ blog ->title = $ blogTitle ;
188
- $ blog ->description = $ blogDescription ;
189
- $ blog ->status = "posted " ;
190
- $ tagIds = [];
191
- foreach ($ tagNames as $ tagName ) {
192
- $ tag = Tag::firstOrCreate (['title ' => $ tagName ]);
193
- if ($ tag ) {
194
- $ tagIds [] = $ tag ->id ;
195
- }
196
- };
197
- $ blog ->save ();
198
- $ blog ->tags ()->sync ($ tagIds );
199
- } else {
200
- $ blog = new Blog ();
201
- $ blog ->title = $ blogTitle ;
202
- $ blog ->description = $ blogDescription ;
203
- $ blog ->status = "posted " ;
204
- $ blog ->user_id = auth ()->user ()->id ;
205
- $ tagIds = [];
206
- foreach ($ tagNames as $ tagName ) {
207
-
208
- $ tag = Tag::firstOrCreate (['title ' => $ tagName ]);
209
- if ($ tag ) {
210
- $ tagIds [] = $ tag ->id ;
211
- }
212
- };
213
- $ blog ->save ();
214
- $ blog ->tags ()->sync ($ tagIds );
215
-
216
- $ blogId = $ blog ->id ;
217
- }
218
- return redirect ()->to ("blogs/ " . $ blogId )->with ([
219
- "blog " => $ blog ,
220
- "success " => 'blog created successfully. '
221
- ]);
222
- }
223
- public function edit (Request $ request , $ title )
224
- {
225
- $ titleArray = explode ('- ' , $ title );
226
- $ id = end ($ titleArray );
227
- $ blog = Blog::find ($ id );
157
+ $ blog = Blog::where ("slug " , $ slug )->first ();
228
158
$ this ->Authorize ('view ' , $ blog );
229
159
if ($ blog ) {
230
- $ tagTitles = [];
231
- foreach ($ blog ->tags as $ tag ) {
232
- $ tagTitles [] = $ tag ->title ;
233
- }
234
- return view ("blogs.update " )->with ([
235
- "blog " => $ blog ,
236
- "tagTitles " => json_encode ($ tagTitles ),
237
- ]);
160
+ return view ("blogs.update " )->with (["blog " => $ blog ]);
238
161
}
239
162
}
240
- public function editStore (Request $ request )
241
- {
242
- if (auth ()->user ()->id == $ request ->get ('user_id ' )) {
243
- $ blogId = $ request ->get ('blog_id ' );
244
- $ blogTitle = $ request ->get ('title ' );
245
- $ blogDescription = $ request ->get ('description ' );
246
- $ tagNames = json_decode ($ request ->get ('tags ' ));
247
163
248
- $ blog = Blog::find ($ blogId );
249
- $ blog ->title = $ blogTitle ;
250
- $ blog ->description = $ blogDescription ;
251
- foreach ($ tagNames as $ tagName ) {
252
-
253
- $ tag = Tag::firstOrCreate (['title ' => $ tagName ]);
254
- if ($ tag ) {
255
- $ tagIds [] = $ tag ->id ;
256
- $ tagTitles [] = $ tag ->title ;
257
- }
258
- };
259
- $ blog ->tags ()->sync ($ tagIds );
260
- $ blog ->save ();
261
- $ comments = Comment::where ("blog_id " , "= " , $ blogId )->paginate (5 )->fragment ('comments ' );
262
-
263
- return redirect ()->to ("blogs/ " . $ blogId )->with ([
264
- "blog " => $ blog ,
265
- "comments " => $ comments ,
266
- "success " => 'blog updated successfully. '
267
- ]);
268
- }
269
- return view ("error " );
270
- }
271
- public function manage (Request $ request , $ title )
164
+ public function manage ($ slug )
272
165
{
273
- $ titleArray = explode ('- ' , $ title );
274
- $ id = end ($ titleArray );
275
- $ blog = Blog::find ($ id );
166
+ $ blog = Blog::where ("slug " , $ slug )->first ();
167
+ $ this ->Authorize ('update ' , $ blog );
276
168
if ($ blog ) {
277
- if (auth ()->user ()->id == $ blog ->user_id ) {
278
- return view ("blogs.manage " )->with ([
279
- "blog " => $ blog ,
280
- ]);
281
- }
169
+ return view ("blogs.manage " )->with ([
170
+ "blog " => $ blog ,
171
+ ]);
282
172
}
283
- return view ( " error " );
173
+ return abort ( 404 );
284
174
}
285
175
public function seo (Request $ request )
286
176
{
@@ -299,20 +189,16 @@ public function seo(Request $request)
299
189
}
300
190
return view ("error " );
301
191
}
302
- public function stats (Request $ request , $ title )
192
+ public function stats ($ slug )
303
193
{
304
- $ titleArray = explode ('- ' , $ title );
305
- $ id = end ($ titleArray );
306
- $ blog = Blog::find ($ id );
194
+ $ blog = Blog::where ("slug " , $ slug )->first ();
195
+ $ this ->Authorize ('update ' , $ blog );
307
196
if ($ blog ) {
308
-
309
- if (auth ()->user ()->id == $ blog ->user_id ) {
310
- return view ("blogs.stats " )->with ([
311
- "blog " => $ blog ,
312
- ]);
313
- }
197
+ return view ("blogs.stats " )->with ([
198
+ "blog " => $ blog ,
199
+ ]);
314
200
}
315
- return view ( " error " );
201
+ return abort ( 404 );
316
202
}
317
203
public function manageStore (Request $ request )
318
204
{
@@ -332,42 +218,11 @@ public function manageStore(Request $request)
332
218
}
333
219
return view ("404 " );
334
220
}
335
- public function detailCard (Request $ request )
336
- {
337
- $ blogId = $ request ->get ('blogId ' );
338
-
339
- $ blog = Blog::query ()->where ('id ' , '= ' , $ blogId )->first ();
340
-
341
- $ html = view ("blogs.popover " )
342
- ->with ([
343
- "blog " => $ blog ,
344
- ])->render ();
345
- return json_encode ($ html );
346
- }
347
- public function tagSearch (Request $ request , $ title )
221
+ public function tagSearch (Request $ request , $ slug )
348
222
{
349
- $ searchTag = Tag::where ("title " , "= " , $ title )->first ();
350
- // if ($request->tab == 'likes') {
351
- // $blogs = Blog::where("status", "=", "posted")->whereHas('tags', function ($q) use ($title) {
352
- // $q->where('title', $title);
353
- // })->withCount(['bloglikes' => function ($q) {
354
- // $q->where('status', '=', 1);
355
- // }])->orderByDesc('bloglikes_count')->paginate(10);
356
- // } else if ($request->tab == 'newest') {
357
- // $blogs = Blog::where("status", "=", "posted")->whereHas('tags', function ($q) use ($title) {
358
- // $q->where('title', $title);
359
- // })->orderByDesc('created_at')->paginate(10);
360
- // } else if ($request->tab == 'views') {
361
- // $blogs = Blog::where("status", "=", "posted")->whereHas('tags', function ($q) use ($title) {
362
- // $q->where('title', $title);
363
- // })->withCount('blogviews')->orderByDesc('blogviews_count')->paginate(10);
364
- // } else {
365
- // $blogs = Blog::where("status", "=", "posted")->whereHas('tags', function ($q) use ($title) {
366
- // $q->where('title', $title);
367
- // })->orderByDesc('created_at')->paginate(10);
368
- // }
223
+ $ searchTag = Tag::where ("title " , "= " , $ slug )->first ();
369
224
return view ("blogs.tagged " )->with ([
370
- "searchTag " => $ searchTag
225
+ "searchTag " => $ searchTag
371
226
]);
372
227
}
373
228
0 commit comments