title: Transforming Laravel collections with nested resources
body: <p>This is for Laravel 5.2. I'm aware of Fractal, but I'm trying to learn how to do this without a third party package specifically to have a deep understanding of how Laravel works. With that said, I've been stuck on this for at least the last two days and I am now raising the white flag.</p>
  
  <p>I'm trying to nest resources and run each parent and child resource through specific transformers. The relationships are setup correctly, but I'm pretty sure the issue is with how the collections are being parsed/interpreted/something. Here's an example of something that is blowing my mind:</p>
  
  <p>Get some data from the database and include two relationships (created_by and updated_by are foreign key relationships to another table):</p>
  
  <pre><code>&gt;&gt;&gt; $items = $items::with('created_by')-&gt;with('updated_by')-&gt;where('user_id',30)-&gt;first()
  =&gt; App\Models\User\UserEmail {#1065
       id: 60,
       email: "kShields@hotmail.com",
       type_id: 25,
       created_by: App\Models\User\User {#1055
         id: 30,
         name_first: "Karine",
         name_first_preferred: null,
         name_last: "Isabelle",
       },
       updated_by: App\Models\User\User {#1054
         id: 30,
         name_first: "Karine",
         name_first_preferred: null,
         name_last: "Isabelle",
       },
       created_at: "2015-09-03 04:17:57",
       updated_at: "2017-01-13 08:06:18",
     }
  </code></pre>
  
  <p>So far, so good. The user that created and updated the email was fetched as directed in the UserEmail model as follows:</p>
  
  <pre><code>public function created_by()
  {
      return $this-&gt;hasOne(User::class, 'id', 'created_by')-&gt;withTrashed()-&gt;select(User::getMinimalUserFields());
  }
  
  public function updated_by()
  {
      return $this-&gt;hasOne(User::class, 'id', 'updated_by')-&gt;withTrashed()-&gt;select(User::getMinimalUserFields());
  }
  </code></pre>
  
  <p>Now the part that has me thoroughly confused. I'm able to get at specific pieces of the $items collection in an expected manner, but not others. Checking the <code>id</code> returns 60 - okay, good. Checking <code>created_by</code> returns an integer instead of an object. What?! Checking <code>created_at</code> gives me a Carbon\carbon instance, which means it must have automatically turned itself into a string in the previous output. What?!</p>
  
  <pre><code>&gt;&gt;&gt; $items['id']
  =&gt; 60
  &gt;&gt;&gt; $items['created_by']
  =&gt; 30
  &gt;&gt;&gt; $items-&gt;created_by
  =&gt; 30
  &gt;&gt;&gt; $items-&gt;created_at
  =&gt; Carbon\Carbon {#811
       +"date": "2015-09-03 04:17:57.000000",
       +"timezone_type": 3,
       +"timezone": "UTC",
     }
  </code></pre>
  
  <p>I was expecting to see an object when I tried at least one of the methods shown above to get data out of <code>created_by</code>. My actual problem is in a transformer, but I think the above represents where I'm stuck. I've read every piece of information I can find about collections and transformers, but I either haven't read the right information or I didn't understand it.</p>
  
  <p>This is what I'm actually trying to do, but nothing I've tried has worked. The resulting output is an error due to not being able to call <code>all()</code> on an integer because the <code>id</code> of the user is being passed into the sub-resource transformer instead of an object.</p>
  
  <pre><code>public function transform($email)
  {
      $data = [
          'id'         =&gt; (integer)$email['id'],
          'email'      =&gt; $email['email'],
          'type_id'    =&gt; (integer)$email['type_id'],
          'created_at' =&gt; $email['created_at'],
          'updated_at' =&gt; $email['updated_at'],
      ];
  
      if (isset($email-&gt;created_by)) {
          $userMinimalTransformer = new UserMinimalTransformer();
          $data['created_by'] = $userMinimalTransformer-&gt;transformCollection($email-&gt;created_by-&gt;all());
      }
  
      return $data;
  }
  </code></pre>
  
  <p>Any help would be greatly appreciated.</p>
  
  <p>Edit:</p>
  
  <p>In response to a comment, here's another example of not being able to get the user object from <code>updated_by</code> when the ->all() method is called on the paginator object:</p>
  
  <pre><code>&gt;&gt;&gt; $n = App\Models\User\UserNote::with('updated_by')-&gt;paginate(2)
  =&gt; Illuminate\Pagination\LengthAwarePaginator {#879}
  &gt;&gt;&gt; $n-&gt;all()
  =&gt; [
       App\Models\User\UserNote {#869
         id: 1,
         note: "Dolorum amet iste laborum eius est dolor dolores minus voluptatem quisquam.",
         type_id: 100,
         created_by: 1,
         updated_by: App\Models\User\User {#856
           id: 1,
           name_first: "System",
           name_first_preferred: "System",
           name_last: "System",
         },
         created_at: "2015-06-01 22:54:14",
         updated_at: "2017-01-11 15:10:08",
       },
       App\Models\User\UserNote {#964
         id: 2,
         note: "Quo sed fugit facilis perferendis dolores molestias ipsam sit veniam sed fuga aspernatu
  r natus.",
         type_id: 125,
         created_by: 1,
         updated_by: App\Models\User\User {#856},
         created_at: "2015-09-19 16:10:19",
         updated_at: "2016-08-28 16:03:58",
       },
     ]
  &gt;&gt;&gt; $n[0]
  =&gt; App\Models\User\UserNote {#869
       id: 1,
       note: "Dolorum amet iste laborum eius est dolor dolores minus voluptatem quisquam.",
       type_id: 100,
       created_by: 1,
       updated_by: App\Models\User\User {#856
         id: 1,
         name_first: "System",
         name_first_preferred: "System",
         name_last: "System",
       },
       created_at: "2015-06-01 22:54:14",
       updated_at: "2017-01-11 15:10:08",
     }
  &gt;&gt;&gt; $n[0]['id']
  =&gt; 1
  &gt;&gt;&gt; $n[0]['updated_by']
  =&gt; 1
  &gt;&gt;&gt; $n[0]-&gt;updated_by
  =&gt; 1
  </code></pre>