title: Laravel resource conditional return
body: <p>I've got simple laravel resource: </p>
  
  <pre><code>&lt;?php
  
  namespace App\Http\Resources;
  
  use Illuminate\Http\Resources\Json\JsonResource;
  
  class UserResource extends JsonResource
  {
      /**
       * Transform the resource into an array.
       *
       * @param  \Illuminate\Http\Request  $request
       * @return array
       */
      public function toArray($request)
      {
          return [
              'id' =&gt; $this-&gt;id,
              'unread' =&gt; $this-&gt;unread,
              'details' =&gt; new EmployeeAddressResource($this-&gt;employeeAddress),
          ];
      }
  }
  </code></pre>
  
  <p>This is working fine, now I want to make details conditional :</p>
  
  <pre><code>       'details' =&gt; $this
          -&gt;when((auth()-&gt;user()-&gt;role == 'company'), function () {
                 return new EmployeeAddressResource($this-&gt;employeeAddress);
                  }),
  </code></pre>
  
  <p>and it works fine too, but how can I add other condition to return other resource? For exaple, if role is <code>user</code> i want to get resource: <code>CompanyAddressResource</code></p>
  
  <p>I tried this:</p>
  
  <pre><code>       'details' =&gt; $this
          -&gt;when((auth()-&gt;user()-&gt;role == 'company'), function () {
                      return new EmployeeAddressResource($this-&gt;employeeAddress);
                  })
          -&gt;when((auth()-&gt;user()-&gt;role == 'user'), function () {
                      return new CompanyAddressResource($this-&gt;companyAddress);
                  }),
  </code></pre>
  
  <p>but this is not working, when I'm logged as <code>company</code> it gives no <code>details</code> </p>
  
  <p>How can I make this work?</p>