今日已更新 215 条资讯 | 累计 21081 条内容
关于我们

Laravel API data envelope

/u/Totoro-Caelum 2026年06月04日 22:40 4 次阅读 来源:Reddit r/webdev

i'm having a hard time deciding which approach i should implement. i'm developing a Laravel api which is consumed by Vue & Nuxt and i didn't noticed that i actually implemented two approaches of the returned response: [1] return ArticleResource::collection($articles); this returns a JSON like this: { "id": 1, "title": "My Article" } [2] return response()->json([ 'data' => new ArticleResource($article), 'success' => true, 'message' => 'OK', ]); JSON: { "data": { // output of ArticleResource transformed $article }, "success": true, "message": "OK" } considering that the API and frontend are private repositories. does wrapping all of the response inside 'data' makes sense or should i just stick on [1] for less nesting? what do you guys think what do you usually do with your years of experience? submitted by /u/Totoro-Caelum [link] [留言]

本文内容来源于互联网,版权归原作者所有
查看原文