So, I have this crazy idea that is related to Laravel and model inheritance. I would like to set up a single-parent model set, but when I ask for a child model, I would like to return the data. For example, I would have a contact model that is parent:
Contacts: id, first_name, last_name, image
Then I will have a series of contact types that inherit from contacts. Each of these child models will have its own set of fields (i.e. for members I need to know when they joined, etc., but for volunteers I may need to know if they have a modern first aid certificate ) Here are some examples:
Members: contact_id, joined_on, birthday, medical_concerns Volunteers: contact_id, current_first_aid, interests Staff: contact_id, pay_rate
I would like to do something like:
$members = \App\Member::all();
and return the contact data AND, as if everything was a single line, for example:
+---+------------+-----------+-------+------------+------------+------------------+ |id | first_name | last_name | image | joined_on | birthday | medical_concerns | +---+------------+-----------+-------+------------+------------+------------------+ | 1 | Fred | Bloggs | null | 2015-01-01 | 1993-10-22 | false | | 2 | Jim | Phillips | null | 2016-04-30 | 1987-09-22 | true | +---+------------+-----------+-------+------------+------------+------------------+
And to make it a little more complicated, I would like all the relationships that apply to parents to work for the child. So I could do something like this:
$members = \App\Member::find(1)->phone
And even if the Member model does not have a relationship defined for the Phone model, it will return the phone associated with the contact, because the parent has this relationship.
I would also like to indicate columns that are not related to the child when retrieving data, and Laravel does not throw an error:
$members = \App\Member::all(['first_name','last_name','joined_on'])
I came across overriding the Eloquent model and writing my own version of all and finding methods that work, but it looks like I might have to redefine all the methods to make it work, and maybe it will work more than just leave Eloquent and looking for another (or my own custom solution).
So, I think, my questions are: is there a โsimpleโ way to do this with Laravel, or am I trying to get him to do what he never intended to do?