부모
커밋
346100a77e
2개의 변경된 파일8개의 추가작업 그리고 21개의 파일을 삭제
  1. 7
    20
      app/Http/Controllers/Api/NewsController.php
  2. 1
    1
      database/migrations/2025_05_20_043733_create_news_table.php

+ 7
- 20
app/Http/Controllers/Api/NewsController.php 파일 보기

@@ -116,7 +116,10 @@ class NewsController extends Controller
116 116
         $locate = $locale == "tw" ? "zh_TW" : $locale;
117 117
         $news = News::find($id);
118 118
         $otherNewsList = [];
119
-
119
+        if(!$news){
120
+            return response()->json([
121
+                'message' => "news not found"], 404);
122
+        }
120 123
         //取得前一篇
121 124
         $previous = News::where(function ($query) use ($news) {
122 125
             $query->where('order', $news->order)
@@ -167,7 +170,7 @@ class NewsController extends Controller
167 170
             "id" => $next->id,
168 171
             "categoryId" => $next->newsCategory->id,
169 172
             "category" => $next->newsCategory->getTranslation("name", $locate, false),
170
-            "postDate" => Carbon::parse($previous->post_date)->format("Y/m/d"),
173
+            "postDate" => Carbon::parse($next->post_date)->format("Y/m/d"),
171 174
             "title" => $next->getTranslation("title", $locate, false),
172 175
             "imgPC" => $next->news_img_pc_url,
173 176
             "imgMobile" => $next->news_img_mobile_url,
@@ -175,22 +178,6 @@ class NewsController extends Controller
175 178
             "description" => $next->getTranslation("description", $locate, false),
176 179
         ];
177 180
 
178
-
179
-        // {
180
-        //     "type": "text",
181
-        //     "content": ""
182
-        // },
183
-        // {
184
-        //     "type": "image",
185
-        //     "images":[{"img_url":"", "alt":""}, {"img_url":"", "alt":""}]
186
-        // },
187
-        // {
188
-        //     "type": "video",
189
-        //     "is_yt":false,
190
-        //     "video_img_url":"",
191
-        //     "video_url":"",
192
-        //     "content": ""
193
-        // }
194 181
         $paragraphs = [];
195 182
         foreach($news->paragraphs as $paragraph){
196 183
             $content = [];
@@ -236,9 +223,9 @@ class NewsController extends Controller
236 223
             "metaKeyword" => $news->getTranslation("meta_keyword", $locate, false),
237 224
             "metaDesc" => $news->getTranslation("meta_description", $locate, false),
238 225
             "metaImg" => $news->meta_img,
239
-            "paragraphs" => $paragraphs,
240
-            "otherNews" => $otherNewsList
226
+            "paragraphs" => $paragraphs
241 227
         ];
228
+        if(!empty($otherNewsList)) $result["otherNews"] = $otherNewsList;
242 229
         return response()->json(["data" => $result], 200);
243 230
     }
244 231
 }

+ 1
- 1
database/migrations/2025_05_20_043733_create_news_table.php 파일 보기

@@ -22,7 +22,7 @@ return new class extends Migration
22 22
             $table->json('title')->comment("標題");
23 23
             $table->json('description')->comment("短文");
24 24
             $table->json('written_by')->comment("編輯人");
25
-            $table->dateTime('post_date')->comment("文章發布日");
25
+            $table->date('post_date')->comment("文章發布日");
26 26
             $table->string('meta_img')->nullable()->comment('seo image');
27 27
             $table->json("meta_title")->nullable()->comment("seo title");
28 28
             $table->json("meta_keyword")->nullable()->comment("seo keyword");