Blog & Stuff

Many to Many Polymorphic Relations from Laravel to Angular and back

Posted: 2014-07-05 01:36:17

This will cover having a polymorphic relationship in Laravel, showing it on a form in Angular and saving it back to Laravel. Overall it is well documented here at the great docs at Laravel but this post might help in a few areas.

As someone defines Polymorphic Relationships there

"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. "

Output the data to Angular

Taking the User Model as an example I query the user but then before returning the data to Angular I append the polymorphic results. In this case we will call it Specialties

This returns just an array to Angular to deal with. When sending it an Object trouble getting Angular to set the selected options. The hard part, since by default it just worked, was to send not only the 2 selected Specialties but the 20 other possible ones and make the list out of that.