title: Manually build a serializable collection in Laravel
body: <p>I'm programming a <strong>RESTful service</strong> with <strong>Laravel</strong>. Now I have some trouble with serializing a manually created list of objects.</p>
  
  <p>I have a <code>User</code> object that should have an attribute <code>solarpanels</code>. This attribute I realised with an accessor like this:</p>
  
  <pre><code>function getSolarplantsAttribute() {
      $solarplants = array();
      $mastergrants = MasterGrant::where('userid', '=', $this-&gt;id)-&gt;get();
      foreach ($mastergrants as $mastergrant) {
          $masternode = Masternode::find($mastergrant-&gt;masternodeid);
          if ($masternode == null) {
              continue;
          }
          $solarplant = SolarPlant::find($masternode-&gt;fieldID);
          $solarplants[] = $solarplant;
      }
      return $solarplants;
  }
  </code></pre>
  
  <p>Unfortunately, I have to do it in this complicated fashion, as that's how the database scheme is designed and I can't change it.</p>
  
  <p>Now I have a <strong>Resource Controller</strong>, that I wanted to realise in this way:</p>
  
  <pre><code>public function index()
  {
      $user = Auth::user();
      $solarplants = $user-&gt;solarplants;
      return $solarplants;
  }
  </code></pre>
  
  <p>Unfortunately, this way the objects get serialized as:</p>
  
  <pre><code>[{"timestamps":false,"incrementing":true,"exists":true}]
  </code></pre>
  
  <p>This is wrong, it's only seralizing the object variables, but not how it should be done and how I designed the <strong>Eloquent Object</strong>. So what I tried, is to do this:</p>
  
  <pre><code>public function index()
  {
      $user = Auth::user();
      $solarplants = $user-&gt;solarplants;
      $convertedPlants = array();
      foreach ($solarplants as $solarplant) {
          $convertedPlants[] = $solarplant-&gt;toJson();
      }
      return $convertedPlants;
  }
  </code></pre>
  
  <p>This kind of worked, but now Laravel is escaping my JSON string, which does change the JSON's meaning and thus is <em>not processable anymore</em> in the frontend. I also tried to cast it like this: <code>$solarplant->toArray()</code>, but then the <strong>accessors</strong> of the <code>SolarPlant</code> object get ignored.</p>
  
  <p>Then I had another idea (as when I do <code>return SolarPlant::all();</code> works and gets serialized perfectly fine). I wanted to create a <strong>Collection</strong> manually. But this again results in a way that the accessors don't get properly serialized.</p>
  
  <p>So basically, I have no proper solution for my problem yet. Does any of you have an advice?</p>
  
  <p><strong>EDIT for simplification:</strong></p>
  
  <p>I try to explain the core of the problem a bit more simple and abstract. My <code>SolarPlant</code> object is an eloquent object, that has accessors. This accessors themselves return single eloquent objects or lists of eloquent objects. Now when I do this, everything works fine:</p>
  
  <pre><code>Route::get('test', function () {
      return SolarPlant::find(1);
  });
  </code></pre>
  
  <p>But this way, the serialization doesn't work anymore, it's serializing the raw php objects instead of doing it in the eloquent way:</p>
  
  <pre><code>Route::get('test', function () {
    $solarplants = array();
    $solarplants[] = SolarPlant::find(1);
    $solarplants[] = SolarPlant::find(1);
    return $solarplants;
  });
  </code></pre>
  
  <p>But what I also just figured out, is that the SolarPlant::all() doesn't work properly as I expected. The accessors don't get processed well too.</p>