Polymorphic Eloquent relationships with namespaces

eloquent, laravel, laravel-4, polymorphism

Solution

Since Laravel 4.1, inside your model (in this case Company and People) you can set the protected property `$morphClass` to whatever you want.

<?php namespace Lion;

class Company extends \Eloquent { 

    protected $morphClass = 'Company';
}

Now in your table you can store the type without the namespace:

 |  id  |  realatable_type  |  relatable_id
 |  2   |  Company          |  13

Problem

I've tried to implement polymorphic relationships. They work perfectly... However, I'm trying to reduce my database size as much as possible so... I've this ``` Table action | id | realatable_type | relatable_id | 1 | Lion\People | 65 | 2 | Lion\Company | 13 ``` Obviously I've this ``` <?php namespace Lion; class Company extends \Eloquent { ... } class People extends \Eloquent { ... } ``` Is there any way to store only "People" or "Company" assuming that the namespace is always going to be "Lion"?

Original source