title: Laravel Custom Wrapper for API Responses
body: <p>I am trying to structure my project in <code>Laravel</code> just created to use it as a <code>back-end API</code>. I want all my responses from <code>Laravel</code> to be returned in the <code>JSON:API</code> format defined on the official site: <a href="https://jsonapi.org/format/" rel="nofollow noreferrer">https://jsonapi.org/format/</a></p>
  <p>For example:
  I have created the following 2 resource files:</p>
  <p>1- <code>php artisan make:resource User</code></p>
  <p>2- <code>php artisan make:resource UserCollection --collection</code></p>
  <p>Two simple resource files, one to return a <code>resource</code> and one to return a <code>collection</code>.</p>
  <p>Now, I would like to return in all my responses the following format:</p>
  <ul>
  <li>In case of <code>success</code></li>
  </ul>
  <p>1- The status return code can be <code>200</code>, <code>201</code> or <code>202</code>.</p>
  <p>2- The returned response should be similar to the following:</p>
  <pre><code>{
      &quot;data&quot;: [
          {
              &quot;id&quot;: 1,
              &quot;email&quot;: &quot;collins.elody@example.org&quot;
          }
      ],
      &quot;message&quot;: null,
      &quot;success&quot;: true
  }
  </code></pre>
  <p>You may be wondering what is the point of passing the <code>message</code> key, in this case it is <code>null</code> because it would be returning a <code>collection</code> of records, that is, <strong>reading</strong>, but in case you needed to <strong>add</strong> a new record, <strong>update</strong> or <strong>delete</strong> one, you would need to pass a message to my <code>front-end</code>, in this case I would use that key for that.</p>
  <p>Example, <strong>adding</strong> record, response status code <code>201</code>:</p>
  <pre><code>{
      &quot;data&quot;: [],
      &quot;message&quot;: &quot;Record created succesfully&quot;,
      &quot;success&quot;: true
  }
  </code></pre>
  <ul>
  <li>In case of <code>failure</code></li>
  </ul>
  <p>As said here: <a href="https://jsonapi.org/format/#document-top-level" rel="nofollow noreferrer">https://jsonapi.org/format/#document-top-level</a> : <em>The members data and errors MUST NOT coexist in the same document.</em></p>
  <p>So, in case of error, I need change <code>data</code> key by <code>errors</code> key, for example, suppose I am trying to authenticate myself, and the validation fails, in this case, it should turn out like this:</p>
  <pre><code>{
      &quot;errors&quot;: {
          &quot;email&quot;: &quot;E-Mail is required&quot;,
          &quot;password&quot;: &quot;Password is required&quot;
      },
      &quot;message&quot;: null,
      &quot;success&quot;: false
  }
  </code></pre>
  <p>or I just want to return an <code>error message</code>, expected output should by:</p>
  <pre><code>{
      &quot;errors&quot;: [],
      &quot;message&quot;: &quot;Something is Wrong!&quot;,
      &quot;success&quot;: false
  }
  </code></pre>
  <p>So in essence what I need is a global wrapper for all the responses I make from Laravel. I would like to call return in an elegant way as follows:</p>
  <pre><code>return $this-&gt;success($collection);
  </code></pre>
  <p>or</p>
  <pre><code>return $this-&gt;success('Done!', 201);
  </code></pre>
  <p>So the first thing that came to mind was creating a <code>trait</code> and defining the methods you need to then call them from anywhere in <code>Laravel</code></p>
  <p>My <code>Trait</code></p>
  <pre><code>&lt;?php
  
  namespace App\Traits;
  
  trait APIResponse
  {
      public function success($data, $status = 200) {
          return [
              'data' =&gt; $data,
              'success' =&gt; in_array($status, [200, 201, 202]) ? true : false,
              'message' =&gt; null
          ];
      }
  
      public function failure($data, $status = 500) {
          // TODO
      }
  }
  
  </code></pre>
  <p>My <code>Controller</code></p>
  <pre><code>class ExampleController extends Controller
  {
      public function index() {
  
          $collection = new UserCollection(User::all());
  
          return $this-&gt;success($collection);
      }
  }
  
  </code></pre>
  <p>But I am not sure it is the right way to do it, please, someone skilled in the field who can help me. Thank you very much in advance.</p>