Today I needed to implement single table inheritance with Laravel 4.
Single table inheritance, in a nutshell, is where you have a hierarchy of classes stored in a single table. It’s a great pattern in a situation where most of the fields of the classes are the same but only a couple are different in the child classes.
In my case I have a base Contact class with two descendant classes, Customer and Vendor.
Contact contains the company name, contact name, addresses, phone numbers, etc. Customer extends Contact with fields like sales_tax_id where Vendor extends Contact with fields like terms_id.
With single table inheritance this all ends up in the contacts table like so:
Continue reading