Skip to content

Commit

Permalink
Merge pull request #116 from ihamzehald/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
ihamzehald authored Oct 3, 2020
2 parents acccac4 + dee1509 commit 2549293
Showing 1 changed file with 26 additions and 1 deletion.
27 changes: 26 additions & 1 deletion app/Http/Helpers/ApiResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,38 @@ trait ApiResponse
* @param $data array/null as response data
* @param null $errors
* @return \Illuminate\Http\JsonResponse
* Note: $errors param expected to be Illuminate\Support\MessageBag object
* or an array in this form
* [
* "error_key":[
* "err_msg_1",
* "err_msg_2"
* ]
* ]
*
*/
public function sendResponse($status, $message, $data = null, $errors = null)
{

$errors = $errors instanceof MessageBag ? $errors->messages() : $errors;

$errorsList = [];

if($errors){

foreach ($errors as $errorKey => $error){
$errorList = [$errorKey => $error];
$errorsList[] = $errorList;
}

}

$errorsList = !empty($errorsList) ? $errorsList : null;

return response()->json([
"message" => $message,
"data" => $data,
"errors" => $errors
"errors" => $errorsList
], $status);
}
}

0 comments on commit 2549293

Please sign in to comment.