http://laravel.com/docs/eloquent#polymorphic-relations
Polymorphic relations allow a model to belong to more than one other model, on a single association. For example, you might have a photo model that belongs to either a staff model or an order model.
I don't really understand this use case. Does this mean that a member of staff could own a photo, or the photo could be placed in an order? Why not just put photo_id
in staff
table and photo_id
in order
table?
Could somebody please provide an alternative, simple to understand, example where polymorphic relations may be used?
You can add the photo_id
to Staff and Order, but with the morphMany relation type you don't have to 'clutter' all the models that are needing a Photo.
When creating a third model that needs a Photo, just add the methods and you are done - no need for creating extra columns what so ever.
Also note you can specify the name. The manual uses 'imageable', but feel free to use multiple names for different types of Photo's (or other models)