Parent and Grandchildren classes in Laravel, using same table?

Posted 6 days ago by KimG

Hi there!

I am rewriting an old program into Laravel at the moment.

The old program had items in it. There was a parent class, Items, and then a number of child classes such as: Weapon, HealingItem, etc, that added additional capabilities. Then there were "grandchildren" classes such as Battle Axe and Whip, which sometimes added additional capabilities but most of the time added flavor text to the existing capabilities of their parent classes.

There was a single database table for all of the items. This table had one column, type, which would determine which of the many grandchildren were instantiated when an item was looked up by ID (or by who owned it in the case of getting a collection of items)

What would be the cleanest way to rewrite this into Laravel? I can make an Item model, but it's not clear to me how I ought to go about "morphing" that item's behaviors based on a "type" column. All of the methods I've considered feel... kinda hackey and hard to maintain.

Please sign in or create an account to participate in this conversation.

Reply to

Use Markdown with GitHub-flavored code blocks.