title: Laravel API Response Object formatting
body: <p>I have created a basic Laravel REST API that will service some JavaScript applications that are using axios to make requests.</p>
  
  <p>My application is using a Base Controller to ensure all responses are sent in the same format:</p>
  
  <pre><code>class BaseController extends Controller
  {
      /**
       * success response method.
       *
       * @param $result
       * @param $message
       *
       * @return JsonResponse
       */
      public function sendResponse($result, $message)
      {
          $response = [
              'success' =&gt; true,
              'data' =&gt; $result,
              'message' =&gt; $message,
          ];
  
          return response()-&gt;json($response, 200);
      }
  
      /**
       * return error response.
       *
       * @param $error
       * @param  array  $errorMessages
       * @param  int  $code
       *
       * @return JsonResponse
       */
      public function sendError($error, $errorMessages = [], $code = 200)
      {
          $response = [
              'success' =&gt; false,
              'message' =&gt; $error,
          ];
  
          if (!empty($errorMessages)) {
              $response['data'] = $errorMessages;
          }
  
          return response()-&gt;json($response, $code);
      }
  }
  </code></pre>
  
  <p>When I retrieve the response from the API URL and log it to the console, I get the following output:</p>
  
  <p><a href="https://i.stack.imgur.com/xAEr1.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/xAEr1.png" alt="enter image description here"></a></p>
  
  <p>Currently, my response object contains its own data object, which then contains another data object. As a result, to access the data I have to use response.data.data.</p>
  
  <p>I am wondering if this is actually the correct way to do this. As the naming convention of response.data.data doesn't seem to be a very clean way to do this. I have seen a couple of guides online where it has been done this way, but I am wondering if there are any typically followed conventions that I am missing. Perhaps this is correct but I just wanted some insight from more experienced programmers.</p>