[{"data":1,"prerenderedAt":847},["ShallowReactive",2],{"\u002Farticles\u002Flaravel-api-response-formatter":3,"\u002Farticles\u002Flaravel-api-response-formatter-surroundings":838},{"id":4,"title":5,"author":6,"body":7,"createdAt":818,"description":819,"extension":820,"img":821,"meta":822,"navigation":271,"path":824,"publishedAt":825,"readingTime":826,"seo":831,"stem":832,"tags":833,"updatedAt":836,"__hash__":837},"articles\u002Farticles\u002F2.laravel-api-response-formatter.md","Laravel API Response Formatter","Rendi Okriza Putra",{"type":8,"value":9,"toc":811},"minimark",[10,17,22,35,39,42,67,70,74,80,190,194,200,234,238,244,609,620,729,732,794,804,807],[11,12,13,16],"p",{},[14,15,5],"code",{}," is a class that provides methods for formatting API responses in a standardized format. It simplifies the process of creating consistent and well-structured JSON responses in your API.",[18,19,21],"h2",{"id":20},"requirements","Requirements",[23,24,25,32],"ul",{},[26,27,28,29],"li",{},"PHP ",[14,30,31],{},"^7.4 | ^8.0",[26,33,34],{},"Laravel 6, 7, 8, 9 or 10",[18,36,38],{"id":37},"installation","Installation",[11,40,41],{},"You can install the package via composer:",[43,44,49],"pre",{"className":45,"code":46,"language":47,"meta":48,"style":48},"language-bash shiki shiki-themes one-dark-pro","composer require okriiza\u002Flaravel-api-response-formatter\n","bash","",[14,50,51],{"__ignoreMap":48},[52,53,56,60,64],"span",{"class":54,"line":55},"line",1,[52,57,59],{"class":58},"sVbv2","composer",[52,61,63],{"class":62},"subq3"," require",[52,65,66],{"class":62}," okriiza\u002Flaravel-api-response-formatter\n",[11,68,69],{},"The package will automatically register itself.",[18,71,73],{"id":72},"function-list","Function List",[11,75,76,77,79],{},"The ",[14,78,5],{}," class provides the following functions:",[81,82,83,96],"table",{},[84,85,86],"thead",{},[87,88,89,93],"tr",{},[90,91,92],"th",{},"Function",[90,94,95],{},"Description",[97,98,99,110,120,130,140,150,160,170,180],"tbody",{},[87,100,101,107],{},[102,103,104],"td",{},[14,105,106],{},"success()",[102,108,109],{},"Formats a success response with optional data, message, status, and HTTP code.",[87,111,112,117],{},[102,113,114],{},[14,115,116],{},"created()",[102,118,119],{},"Formats a created response with optional data, message, status, and HTTP code.",[87,121,122,127],{},[102,123,124],{},[14,125,126],{},"noContent()",[102,128,129],{},"Formats a no content response with optional data, message, status, and HTTP code.",[87,131,132,137],{},[102,133,134],{},[14,135,136],{},"error()",[102,138,139],{},"Formats an error response with optional data, message, status, and HTTP code.",[87,141,142,147],{},[102,143,144],{},[14,145,146],{},"unAuthenticated()",[102,148,149],{},"Formats an unauthenticated response with optional data, message, status, and HTTP code.",[87,151,152,157],{},[102,153,154],{},[14,155,156],{},"forbidden()",[102,158,159],{},"Formats a forbidden response with optional data, message, status, and HTTP code.",[87,161,162,167],{},[102,163,164],{},[14,165,166],{},"notFound()",[102,168,169],{},"Formats a not found response with optional data, message, status, and HTTP code.",[87,171,172,177],{},[102,173,174],{},[14,175,176],{},"methodNotAllowed()",[102,178,179],{},"Formats a method not allowed response with optional data, message, status, and HTTP code.",[87,181,182,187],{},[102,183,184],{},[14,185,186],{},"failedValidation()",[102,188,189],{},"Formats a failed validation response with optional data, message, status, and HTTP code.",[18,191,193],{"id":192},"parameters","Parameters",[11,195,196,197,199],{},"The functions in the ",[14,198,5],{}," class accept the following parameters:",[23,201,202,208,214,228],{},[26,203,204,207],{},[14,205,206],{},"$data"," (optional): The data to be included in the response. It can be of any type.",[26,209,210,213],{},[14,211,212],{},"$message"," (optional): The message to be included in the response. If not provided, a default message will be used.",[26,215,216,219,220,223,224,227],{},[14,217,218],{},"$status"," (optional): The success status of the response. Defaults to ",[14,221,222],{},"true"," for success responses and ",[14,225,226],{},"false"," for error responses.",[26,229,230,233],{},[14,231,232],{},"$httpCode"," (optional): The HTTP response code to be returned. It defaults to the corresponding HTTP status code for each response type.",[18,235,237],{"id":236},"example-usage","Example Usage",[11,239,240,241,243],{},"Here's an example of how you can use the ",[14,242,5],{}," class in a user controller:",[43,245,250],{"className":246,"code":247,"filename":248,"language":249,"meta":48,"style":48},"language-php shiki shiki-themes one-dark-pro","\u003C?php\n\nuse Okriiza\\ApiResponseFormatter\\ApiResponse;\n\nclass UserController extends Controller\n{\n    public function show($id): JsonResponse\n    {\n        $user = User::find($id);\n\n        if ($user) {\n            return ApiResponse::success($user);\n        } else {\n            return ApiResponse::notFound(null, 'User not found');\n        }\n    }\n\n    public function create(Request $request): JsonResponse\n    {\n        \u002F\u002F Validation logic\n\n        if ($validationFails) {\n            return ApiResponse::failedValidation($validationErrors);\n        }\n\n        $user = User::create($request->all());\n\n        return ApiResponse::created($user);\n    }\n}\n","app\u002Fhttp\u002Fcontrollers\u002FUserController.php","php",[14,251,252,266,273,289,294,309,315,340,346,371,376,391,411,423,449,455,461,466,488,493,500,505,517,536,541,546,574,579,598,603],{"__ignoreMap":48},[52,253,254,258,262],{"class":54,"line":55},[52,255,257],{"class":256},"sjrmR","\u003C",[52,259,261],{"class":260},"seHd6","?",[52,263,265],{"class":264},"sn6KH","php\n",[52,267,269],{"class":54,"line":268},2,[52,270,272],{"emptyLinePlaceholder":271},true,"\n",[52,274,276,279,282,286],{"class":54,"line":275},3,[52,277,278],{"class":260},"use",[52,280,281],{"class":264}," Okriiza\\ApiResponseFormatter\\",[52,283,285],{"class":284},"sU0A5","ApiResponse",[52,287,288],{"class":264},";\n",[52,290,292],{"class":54,"line":291},4,[52,293,272],{"emptyLinePlaceholder":271},[52,295,297,300,303,306],{"class":54,"line":296},5,[52,298,299],{"class":260},"class",[52,301,302],{"class":284}," UserController",[52,304,305],{"class":260}," extends",[52,307,308],{"class":284}," Controller\n",[52,310,312],{"class":54,"line":311},6,[52,313,314],{"class":264},"{\n",[52,316,318,321,324,327,330,334,337],{"class":54,"line":317},7,[52,319,320],{"class":260},"    public",[52,322,323],{"class":260}," function",[52,325,326],{"class":58}," show",[52,328,329],{"class":264},"(",[52,331,333],{"class":332},"sVyAn","$id",[52,335,336],{"class":264},"): ",[52,338,339],{"class":284},"JsonResponse\n",[52,341,343],{"class":54,"line":342},8,[52,344,345],{"class":264},"    {\n",[52,347,349,352,355,358,361,364,366,368],{"class":54,"line":348},9,[52,350,351],{"class":332},"        $user",[52,353,354],{"class":256}," =",[52,356,357],{"class":284}," User",[52,359,360],{"class":264},"::",[52,362,363],{"class":58},"find",[52,365,329],{"class":264},[52,367,333],{"class":332},[52,369,370],{"class":264},");\n",[52,372,374],{"class":54,"line":373},10,[52,375,272],{"emptyLinePlaceholder":271},[52,377,379,382,385,388],{"class":54,"line":378},11,[52,380,381],{"class":260},"        if",[52,383,384],{"class":264}," (",[52,386,387],{"class":332},"$user",[52,389,390],{"class":264},") {\n",[52,392,394,397,400,402,405,407,409],{"class":54,"line":393},12,[52,395,396],{"class":260},"            return",[52,398,399],{"class":284}," ApiResponse",[52,401,360],{"class":264},[52,403,404],{"class":58},"success",[52,406,329],{"class":264},[52,408,387],{"class":332},[52,410,370],{"class":264},[52,412,414,417,420],{"class":54,"line":413},13,[52,415,416],{"class":264},"        } ",[52,418,419],{"class":260},"else",[52,421,422],{"class":264}," {\n",[52,424,426,428,430,432,435,437,441,444,447],{"class":54,"line":425},14,[52,427,396],{"class":260},[52,429,399],{"class":284},[52,431,360],{"class":264},[52,433,434],{"class":58},"notFound",[52,436,329],{"class":264},[52,438,440],{"class":439},"sVC51","null",[52,442,443],{"class":264},", ",[52,445,446],{"class":62},"'User not found'",[52,448,370],{"class":264},[52,450,452],{"class":54,"line":451},15,[52,453,454],{"class":264},"        }\n",[52,456,458],{"class":54,"line":457},16,[52,459,460],{"class":264},"    }\n",[52,462,464],{"class":54,"line":463},17,[52,465,272],{"emptyLinePlaceholder":271},[52,467,469,471,473,476,478,481,484,486],{"class":54,"line":468},18,[52,470,320],{"class":260},[52,472,323],{"class":260},[52,474,475],{"class":58}," create",[52,477,329],{"class":264},[52,479,480],{"class":284},"Request",[52,482,483],{"class":332}," $request",[52,485,336],{"class":264},[52,487,339],{"class":284},[52,489,491],{"class":54,"line":490},19,[52,492,345],{"class":264},[52,494,496],{"class":54,"line":495},20,[52,497,499],{"class":498},"sV9Aq","        \u002F\u002F Validation logic\n",[52,501,503],{"class":54,"line":502},21,[52,504,272],{"emptyLinePlaceholder":271},[52,506,508,510,512,515],{"class":54,"line":507},22,[52,509,381],{"class":260},[52,511,384],{"class":264},[52,513,514],{"class":332},"$validationFails",[52,516,390],{"class":264},[52,518,520,522,524,526,529,531,534],{"class":54,"line":519},23,[52,521,396],{"class":260},[52,523,399],{"class":284},[52,525,360],{"class":264},[52,527,528],{"class":58},"failedValidation",[52,530,329],{"class":264},[52,532,533],{"class":332},"$validationErrors",[52,535,370],{"class":264},[52,537,539],{"class":54,"line":538},24,[52,540,454],{"class":264},[52,542,544],{"class":54,"line":543},25,[52,545,272],{"emptyLinePlaceholder":271},[52,547,549,551,553,555,557,560,562,565,568,571],{"class":54,"line":548},26,[52,550,351],{"class":332},[52,552,354],{"class":256},[52,554,357],{"class":284},[52,556,360],{"class":264},[52,558,559],{"class":58},"create",[52,561,329],{"class":264},[52,563,564],{"class":332},"$request",[52,566,567],{"class":264},"->",[52,569,570],{"class":58},"all",[52,572,573],{"class":264},"());\n",[52,575,577],{"class":54,"line":576},27,[52,578,272],{"emptyLinePlaceholder":271},[52,580,582,585,587,589,592,594,596],{"class":54,"line":581},28,[52,583,584],{"class":260},"        return",[52,586,399],{"class":284},[52,588,360],{"class":264},[52,590,591],{"class":58},"created",[52,593,329],{"class":264},[52,595,387],{"class":332},[52,597,370],{"class":264},[52,599,601],{"class":54,"line":600},29,[52,602,460],{"class":264},[52,604,606],{"class":54,"line":605},30,[52,607,608],{"class":264},"}\n",[11,610,611,612,615,616,619],{},"In the above example, the ",[14,613,614],{},"show()"," method fetches a user by ID and returns a success response if the user exists. If the user is not found, it returns a not found response. The ",[14,617,618],{},"create()"," method performs validation and creates a new user. If the validation fails, it returns a failed validation response. Otherwise, it returns a created response with the created user.",[43,621,625],{"className":622,"code":623,"language":624,"meta":48,"style":48},"language-json shiki shiki-themes one-dark-pro","{\n  \"meta\": {\n    \"code\": 200,\n    \"success\": true,\n    \"message\": \"OK\"\n  },\n  \"result\": {\n    \"id\": 1,\n    \"name\": \"John Doe\",\n    \"email\": \"john@example.com\"\n  }\n}\n","json",[14,626,627,631,639,653,664,674,679,686,698,710,720,725],{"__ignoreMap":48},[52,628,629],{"class":54,"line":55},[52,630,314],{"class":264},[52,632,633,636],{"class":54,"line":268},[52,634,635],{"class":332},"  \"meta\"",[52,637,638],{"class":264},": {\n",[52,640,641,644,647,650],{"class":54,"line":275},[52,642,643],{"class":332},"    \"code\"",[52,645,646],{"class":264},": ",[52,648,649],{"class":439},"200",[52,651,652],{"class":264},",\n",[52,654,655,658,660,662],{"class":54,"line":291},[52,656,657],{"class":332},"    \"success\"",[52,659,646],{"class":264},[52,661,222],{"class":439},[52,663,652],{"class":264},[52,665,666,669,671],{"class":54,"line":296},[52,667,668],{"class":332},"    \"message\"",[52,670,646],{"class":264},[52,672,673],{"class":62},"\"OK\"\n",[52,675,676],{"class":54,"line":311},[52,677,678],{"class":264},"  },\n",[52,680,681,684],{"class":54,"line":317},[52,682,683],{"class":332},"  \"result\"",[52,685,638],{"class":264},[52,687,688,691,693,696],{"class":54,"line":342},[52,689,690],{"class":332},"    \"id\"",[52,692,646],{"class":264},[52,694,695],{"class":439},"1",[52,697,652],{"class":264},[52,699,700,703,705,708],{"class":54,"line":348},[52,701,702],{"class":332},"    \"name\"",[52,704,646],{"class":264},[52,706,707],{"class":62},"\"John Doe\"",[52,709,652],{"class":264},[52,711,712,715,717],{"class":54,"line":373},[52,713,714],{"class":332},"    \"email\"",[52,716,646],{"class":264},[52,718,719],{"class":62},"\"john@example.com\"\n",[52,721,722],{"class":54,"line":378},[52,723,724],{"class":264},"  }\n",[52,726,727],{"class":54,"line":393},[52,728,608],{"class":264},[11,730,731],{},"And for an error case:",[43,733,735],{"className":622,"code":734,"language":624,"meta":48,"style":48},"{\n  \"meta\": {\n    \"code\": 404,\n    \"success\": false,\n    \"message\": \"User not found\"\n  },\n  \"result\": null\n}\n",[14,736,737,741,747,758,768,777,781,790],{"__ignoreMap":48},[52,738,739],{"class":54,"line":55},[52,740,314],{"class":264},[52,742,743,745],{"class":54,"line":268},[52,744,635],{"class":332},[52,746,638],{"class":264},[52,748,749,751,753,756],{"class":54,"line":275},[52,750,643],{"class":332},[52,752,646],{"class":264},[52,754,755],{"class":439},"404",[52,757,652],{"class":264},[52,759,760,762,764,766],{"class":54,"line":291},[52,761,657],{"class":332},[52,763,646],{"class":264},[52,765,226],{"class":439},[52,767,652],{"class":264},[52,769,770,772,774],{"class":54,"line":296},[52,771,668],{"class":332},[52,773,646],{"class":264},[52,775,776],{"class":62},"\"User not found\"\n",[52,778,779],{"class":54,"line":311},[52,780,678],{"class":264},[52,782,783,785,787],{"class":54,"line":317},[52,784,683],{"class":332},[52,786,646],{"class":264},[52,788,789],{"class":439},"null\n",[52,791,792],{"class":54,"line":342},[52,793,608],{"class":264},[11,795,76,796,799,800,803],{},[14,797,798],{},"meta"," object contains information about the response, such as the response code, status, and message. The ",[14,801,802],{},"result"," object holds the actual response data.",[11,805,806],{},"Note: The examples provided are simplified and may require modifications to fit your specific use case",[808,809,810],"style",{},"html pre.shiki code .sVbv2, html code.shiki .sVbv2{--shiki-default:#61AFEF}html pre.shiki code .subq3, html code.shiki .subq3{--shiki-default:#98C379}html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html pre.shiki code .sjrmR, html code.shiki .sjrmR{--shiki-default:#56B6C2}html pre.shiki code .seHd6, html code.shiki .seHd6{--shiki-default:#C678DD}html pre.shiki code .sn6KH, html code.shiki .sn6KH{--shiki-default:#ABB2BF}html pre.shiki code .sU0A5, html code.shiki .sU0A5{--shiki-default:#E5C07B}html pre.shiki code .sVyAn, html code.shiki .sVyAn{--shiki-default:#E06C75}html pre.shiki code .sVC51, html code.shiki .sVC51{--shiki-default:#D19A66}html pre.shiki code .sV9Aq, html code.shiki .sV9Aq{--shiki-default:#7F848E;--shiki-default-font-style:italic}",{"title":48,"searchDepth":268,"depth":268,"links":812},[813,814,815,816,817],{"id":20,"depth":268,"text":21},{"id":37,"depth":268,"text":38},{"id":72,"depth":268,"text":73},{"id":192,"depth":268,"text":193},{"id":236,"depth":268,"text":237},"Mei 28, 2023","a simple package Format API responses throughout your Laravel application.","md","\u002Fimage\u002Farticle\u002Flaravel-api-response-formatter.png",{"layout":823},"blog","\u002Farticles\u002Flaravel-api-response-formatter","2023-05-28",{"text":827,"minutes":828,"time":829,"words":830},"3 min read",2.175,130500,435,{"title":5,"description":819},"articles\u002F2.laravel-api-response-formatter",[834,835],"git","github","Sep 21, 2023","LOOva2ZmwkCxgzs1APY3SxbhlJDy_fMf2BjAauuV5EQ",[839,843],{"title":840,"path":841,"stem":842,"children":-1},"Kumpulan Perintah Git (Git Cheat Sheet)","\u002Farticles\u002Fkumpulan-perintah-git-git-cheat-sheet","articles\u002F1.kumpulan-perintah-git-git-cheat-sheet",{"title":844,"path":845,"stem":846,"children":-1},"Git Commit Guidelines","\u002Farticles\u002Fgit-commit-guidelines","articles\u002F3.git-commit-guidelines",1780235711693]