'Laravel class inheritance / structure / better way?

Im building a doctors surgery system in Laravel (9), which holds details of patients. Each patient can be one of 9 different statuses (basically their journey through the system, from applied, ongoing, discharged etc etc). All fine so far, the database (/model) has a patient_status_id that links them to a patient_status table.

However, on the edit patient screen each patient has a set of actions that can be carried out on them (i.e. discharge, check in, check out, cancel), and each set of actions is bespoke to the 'status' they are in. i.e. a patient who has patient_status_id of 1 can do x, y and z. A patient in patient_status_2 can do x, u and v (ie they can overlap).

I'm uncertain how to structure this properly (efficiently / cleanly) in the class / model structure.

a) I could store the actions in a big switch / match statement in the model i.e.:

public function getUserActions() { switch($this->patient_status_id) { .. }

But with 9 different patient_statuses, this would be a really big function, and secondly if patient_status effects something else (which it will) every 'public function...' would have to have the same big switch / match etc statement.

b) I could do this code in a blade component with the same idea ...

@switch

And have that component be large / unwieldy, or even include multiple sub components, but then I'd have issues with the actions that overlap and repeated code. This also seems a little cumbersome and logic for a model is being held in a view file.

c) I did think (and have implemented) 9 different classes (for each status) and used parental (https://github.com/calebporzio/parental) to deal with single class inheritance. Each 'childStatusModel' inherits the main patient model. They can then have their own

public function getActions()

function, and the code would load them in as needed. This also allows me to have lots of extendability down the line of adding more patient status specific functions (just add them to the children)

This works well, but it comes up short in a few areas. Any obseervers on the main Patient model don't get fired on updates / saves etc, as Laravel (rightly so) treats the child model as its own entity. I could add some more observers to all the children, but this seems overkill, and also some packages don't work nicely (i.e. on an audit package, it doesn't fire, although again I could add the trait to all child models but ideally I'd want the audit log to be linked to 'patient' rather than the child model, as the child model changes statuses in its journey).

There might be some clever observer trick I'm missing here, and I could use polymorphic storing of patient class in the audit log? Maybe ...

d) Something clever I'm not thinking about

Would love to have peoples thoughts on how best to structure all this. Ideally I'd like a one 'patient class' (/model) but also want to keep this clean, well structured and extendable down the line.

Thanks a ton



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source