Skip to content

Commit 18a8b31

Browse files
author
rickon
committed
fix bug
1 parent eb7b97c commit 18a8b31

2 files changed

Lines changed: 17 additions & 3 deletions

File tree

src/Http/Controllers/Api/BlogController.php

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -360,12 +360,26 @@ public function showCategory($id)
360360

361361
public function showCategoryBySlug($seo_url_key)
362362
{
363-
return $this->showCategory(BlogCategory::where('seo_url_key', $seo_url_key)->firstOrFail()->id);
363+
$categoryId = BlogCategory::where('seo_url_key', $seo_url_key)->value('id');
364+
if (!$categoryId) {
365+
return response()->json([
366+
'success' => false,
367+
'message' => '分类不存在'
368+
], 404);
369+
}
370+
return $this->showCategory($categoryId);
364371
}
365372

366373
public function showArticleBySlug($seo_url_key)
367374
{
368-
return $this->showArticle(BlogArticle::where('seo_url_key', $seo_url_key)->firstOrFail()->id);
375+
$articleId = BlogArticle::where('seo_url_key', $seo_url_key)->value('id');
376+
if (!$articleId) {
377+
return response()->json([
378+
'success' => false,
379+
'message' => '文章不存在'
380+
], 404);
381+
}
382+
return $this->showArticle($articleId);
369383
}
370384

371385
// 格式化响应数据

src/Routes/api.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@
6868
// 文章接口
6969
Route::get('articles', 'listArticles')->name('v1.blog.articles.index');
7070
Route::get('articles/{id}', 'showArticle')->name('v1.blog.articles.show');
71-
Route::get('articles/by-slug/{seo_url_key}', 'showCategoryBySlug')->name('v1.blog.categories.show.slug');
71+
Route::get('articles/by-slug/{seo_url_key}', 'showArticleBySlug')->name('v1.blog.categories.show.slug');
7272
});
7373

7474
});

0 commit comments

Comments
 (0)