title: Laravel: Returning the namespaced owner of a polymorphic relation
body: <p>I can find a number of discussions regarding this but no clear solution. Here are two links, although I will cover everything in my own question here.</p>
  
  <p><a href="https://github.com/laravel/framework/issues?q=morphClass%20is%3Aissue%20">Github Issues</a></p>
  
  <p><a href="http://laravel.io/forum/10-23-2014-morphclass-not-used-by-morphto">Laravel.io discussion</a></p>
  
  <h1>Simple Explanation</h1>
  
  <p>This is a simple explanation of my problem for anyone already familiar with Laravel's polymorphic relationships.</p>
  
  <p>When using $morphClass, the contents of $morphClass which is saved in the database as the morph  "type" is used for the classname when trying to find the owner of a polymorphic relation. This results in an error since the whole point of $morphClass is that it is not a fully namespaced name of the class.</p>
  
  <p>How do you define the classname that the polymorphic relationship should use?</p>
  
  <h1>More Detailed Explanation</h1>
  
  <p>This is a more detailed explanation explaining exactly what i'm trying to do and why i'm trying to do it with examples.</p>
  
  <p>When using Polymorphic relationships in Laravel whatever is saved as the "morph_type" type in the database is assumed to be the class.</p>
  
  <p>So in this example:</p>
  
  <pre><code>class Photo extends Eloquent {
  
      public function imageable()
      {
          return $this-&gt;morphTo();
      }
  
  }
  
  class Staff extends Eloquent {
  
      public function photos()
      {
          return $this-&gt;morphOne('Photo', 'imageable');
      }
  
  }
  
  class Order extends Eloquent {
  
      public function photos()
      {
          return $this-&gt;morphOne('Photo', 'imageable');
      }
  
  }
  </code></pre>
  
  <p>The database would look like this:</p>
  
  <pre><code>staff
  
   - id - integer
   - name - string
  
  orders
  
   - id - integer
   - price - integer
  
  photos
  
   - id - integer
   - path - string
   - imageable_id - integer
   - imageable_type - string
  </code></pre>
  
  <p>Now the first row of photos might look like this:</p>
  
  <blockquote>
    <p>id,path,imageable_id,imageable_type</p>
    
    <p>1,image.png,1,Staff</p>
  </blockquote>
  
  <p>Now I can either access the Photo from a Staff model or a Staff member from a Photo model.</p>
  
  <pre><code>//Find a staff member and dump their photos
  $staff = Staff::find(1);
  
  var_dump($staff-&gt;photos);
  
  //Find a photo and dump the related staff member
  $photo = Photo::find(1);
  
  var_dump($photo-&gt;imageable);
  </code></pre>
  
  <p>So far so good. However when I namespace them I run into a problem.</p>
  
  <pre><code>namespace App/Store;
  class Order {}
  
  namespace App/Users;
  class Staff {}
  
  namespace App/Photos;
  class Photo {}
  </code></pre>
  
  <p>Now what's saved in my database is this:</p>
  
  <blockquote>
    <p>id,path,imageable_id,imageable_type</p>
    
    <p>1,image.png,1,App/Users/Staff</p>
  </blockquote>
  
  <p>But I don't want that. That's a terrible idea to have full namespaced class names saved in the database like that!</p>
  
  <p>Fortunately Laravel has an option to set a $morphClass variable. Like so:</p>
  
  <pre><code>class Staff extends Eloquent {
  
      protected $morphClass = 'staff';
  
      public function photos()
      {
          return $this-&gt;morphOne('Photo', 'imageable');
      }
  
  }
  </code></pre>
  
  <p>Now the row in my database will look like this, which is awesome!</p>
  
  <blockquote>
    <p>id,path,imageable_id,imageable_type</p>
    
    <p>1,image.png,1,staff</p>
  </blockquote>
  
  <p>And getting the photos of a staff member works absolutely fine.</p>
  
  <pre><code>//Find a staff member and dump their photos
  $staff = Staff::find(1);
  
  //This works!
  var_dump($staff-&gt;photos);
  </code></pre>
  
  <p>However the polymorphic magic of finding the owner of a photo doesn't work:</p>
  
  <pre><code>//Find a photo and dump the related staff member
  $photo = Photo::find(1);
  
  //This doesn't work!
  var_dump($photo-&gt;imageable);
  
  //Error: Class 'staff' not found
  </code></pre>
  
  <p>Presumably there must be a way to inform the polymorphic relationship of what classname to use when using $morphClass but I cannot find any reference to how this should work in the docs, in the source code or via Google.</p>
  
  <p>Any help?</p>