PHP noob here.
I have an Item model which is responsible for adding items to my cart.. However the items I'm adding are not simple products. Each item type has a complicated formula for generating a customized quote based on the users selections and personal discount, etc.
The simplest way I can think to do this is to have functions for each product within the item model with each unique formula in. However, this will get messy very fast.
What is the best practice for splitting these into separate files and then including those files in the model, just for keeping my code clean and seperate?
So for example I have aircraft, cars, boats as categories, each product within each category has it's own formula but I at least want to keep aircraft formulas separate from boat for ease of reading.
Is it best to create an aircraft model, boats model etc and have them all use the items DB table and then once they are added I can control them all from the Item model?
I may have just answered my own question, but I don't know :D Learning loads but still a noob.
I would suggest two things:
I would extract as much of the business logic as possible from the models - to, say, several formula classes (one for each different formula), which you can easily execute wherever you need to.
If the data you will save differs between each type, you may want to look into...
See the laravel docs on this for specifics.
You would have a Product
model, that has a polymorphic morphTo
relationship (called "type"
, maybe) to several other "product type" models (that all have an inverse morphOne
back to the Product
).
Polymorphic relationships were the first thing i thought of while reading your question, but on re-reading it, the data structure for each type might not be different. If that's the case, ignore the polymorphic bit!