父節點
當前提交
346100a77e

+ 7
- 20
app/Http/Controllers/Api/NewsController.php 查看文件

116
         $locate = $locale == "tw" ? "zh_TW" : $locale;
116
         $locate = $locale == "tw" ? "zh_TW" : $locale;
117
         $news = News::find($id);
117
         $news = News::find($id);
118
         $otherNewsList = [];
118
         $otherNewsList = [];
119
-
119
+        if(!$news){
120
+            return response()->json([
121
+                'message' => "news not found"], 404);
122
+        }
120
         //取得前一篇
123
         //取得前一篇
121
         $previous = News::where(function ($query) use ($news) {
124
         $previous = News::where(function ($query) use ($news) {
122
             $query->where('order', $news->order)
125
             $query->where('order', $news->order)
167
             "id" => $next->id,
170
             "id" => $next->id,
168
             "categoryId" => $next->newsCategory->id,
171
             "categoryId" => $next->newsCategory->id,
169
             "category" => $next->newsCategory->getTranslation("name", $locate, false),
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
             "title" => $next->getTranslation("title", $locate, false),
174
             "title" => $next->getTranslation("title", $locate, false),
172
             "imgPC" => $next->news_img_pc_url,
175
             "imgPC" => $next->news_img_pc_url,
173
             "imgMobile" => $next->news_img_mobile_url,
176
             "imgMobile" => $next->news_img_mobile_url,
175
             "description" => $next->getTranslation("description", $locate, false),
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
         $paragraphs = [];
181
         $paragraphs = [];
195
         foreach($news->paragraphs as $paragraph){
182
         foreach($news->paragraphs as $paragraph){
196
             $content = [];
183
             $content = [];
236
             "metaKeyword" => $news->getTranslation("meta_keyword", $locate, false),
223
             "metaKeyword" => $news->getTranslation("meta_keyword", $locate, false),
237
             "metaDesc" => $news->getTranslation("meta_description", $locate, false),
224
             "metaDesc" => $news->getTranslation("meta_description", $locate, false),
238
             "metaImg" => $news->meta_img,
225
             "metaImg" => $news->meta_img,
239
-            "paragraphs" => $paragraphs,
240
-            "otherNews" => $otherNewsList
226
+            "paragraphs" => $paragraphs
241
         ];
227
         ];
228
+        if(!empty($otherNewsList)) $result["otherNews"] = $otherNewsList;
242
         return response()->json(["data" => $result], 200);
229
         return response()->json(["data" => $result], 200);
243
     }
230
     }
244
 }
231
 }

+ 1
- 1
database/migrations/2025_05_20_043733_create_news_table.php 查看文件

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