title: Laravel 5.6 - Pass additional parameters to API Resource?
body: <p>A Laravel <a href="https://laravel.com/docs/5.6/eloquent-resources" rel="noreferrer">API Resource</a> can be either a single resource or a collection. In some cases, additional parameters are required to be passed to the resource/collection from the controller. Below is a simple example demonstrating the issue using <code>User</code> as a single/collection resource, and a custom <code>$apple</code> parameter to be passed to the resource for output. The  issue  can be seen in the final <code>Output (Collection)</code> below, where for the <code>fruit</code> value, we get an incorrect value of <code>banana</code> for the first user, instead of the correct <code>apple</code> value (which all other users get). It works perfectly for the single output, just not the collection. See below:</p>
  
  <p><strong>Controller with UserResource (Single)</strong></p>
  
  <pre class="lang-php prettyprint-override"><code>    $user = User::first();
      return new UserResource($user, $apple = true); // $apple param passed
  </code></pre>
  
  <p><strong>Controller with UserResource (Collection)</strong></p>
  
  <pre class="lang-php prettyprint-override"><code>    $users = User::limit(3)-&gt;get();
      return UserResource::collection($users, $apple = true); // $apple param passed
  </code></pre>
  
  <p><strong>UserResource</strong></p>
  
  <pre class="lang-php prettyprint-override"><code>    &lt;?php
  
      namespace App\Http\Resources;
      use Illuminate\Http\Resources\Json\JsonResource;
  
      class UserResource extends JsonResource {
          private $apple;
  
          public function __construct($resource, $apple = false) {
              // Ensure we call the parent constructor
              parent::__construct($resource);
              $this-&gt;resource = $resource;
              $this-&gt;apple = $apple; // $apple param passed
          }
  
          public function toArray($request) {
              return [
                  'id'     =&gt; (int) $this-&gt;id, 
                  'name'   =&gt; $this-&gt;name,
                  'fruit'  =&gt; $this-&gt;apple ? 'apple' : 'banana',
              ];
          }
      }
  </code></pre>
  
  <p><strong>Output (Single)</strong></p>
  
  <pre class="lang-php prettyprint-override"><code>    {
          "data": {
              "id": 1,
              "name": "Peter",
              "fruit": "apple" // correct param!
          }
      }
  
  </code></pre>
  
  <p><strong>Output (Collection)</strong></p>
  
  <pre class="lang-php prettyprint-override"><code>    {
          "data": [
              {
                  "id": 1,
                  "name": "Peter",
                  "fruit": "banana" // INCORRECT param!
              },
              {
                  "id": 2,
                  "name": "Lois",
                  "fruit": "apple" // correct param!
              },
              {
                  "id": 3,
                  "name": "Brian",
                  "fruit": "apple" // correct param!
              }
          ]
      }
  </code></pre>
  
  <p>Please note that this is just an example, it can be any amount of random parameters (unrelated to the <code>User</code> collection, but must be passed for output logic), such as a single value <code>read_at</code> timestamp from a different table I want to pass once, and do some logic on it in the resource collection before output (like comparison to a user timestamp), or other parameters passed for additional logic <code>if/else</code> to be performed in the resource file in general to manipulate output of collection. How can this be done?</p>