title: How to map Laravel/Eloquent results to custom class
body: <p>I'm looking for a way to map the results of Laravel / Eloquent database queries to custom classes, rather than the default Eloquent class.</p>
  
  <p>Does Laravel / Eloquent include any built-in facility for doing this? If not, is there a suitable place to 'hook' into the result generation code and do the necessary mapping?</p>
  
  <p>As an example, this is roughly what I'd like to achieve:</p>
  
  <pre><code>class User extends Eloquent {}
  
  class MyUser
  {
      protected $name;
  
      public function getName() {
          return $this-&gt;name;
      }
  
      public function setName($name) {
          $this-&gt;name = $name;
          return $this;
      }
  }
  
  $users = User::all();
  
  // $users should now contain an array of MyUser instances
  </code></pre>
  
  <p><strong>Motivation / Reason for question</strong></p>
  
  <p>The motivation behind this question is to find a way in which queries can produce objects or arrays of objects that are completely framework-independent. This is because the Laravel app in question needs to be able to pass its results to other non-Laravel systems, so hence Plain Old PHP Objects (such as MyUser) make the most sense.</p>