Reputation: 1229
My model relationships are as below:
Beneficiary has-many FamilyMembers
Earner is-a FamilyMember
In plain English, a Beneficiary is the one who heads the family. There are Family Members and 'some of them' are considered as Earners since they brings in income to the family.
Beneficiary {id, name}
FamilyMember {beneficiary_id, id, name}
I modeled this as:
Beneficiary hasMany FamilyMember
FamilyMember belongsTo Beneficiary
Earner hasOne FamilyMember <-- but I think this is wrong
My requirement is to get the name of the Earner (Earner->Member->name).
In my controller, $beneficiary
is available (implicit binding
). Then I load Family Members as:
$beneficiary->load(['familyMembers']);
I need to get the list of Earners and the name of Earner from Member model.
I couldn't come up with a correct way of modelling this. If someone can help me how should I arrange the relationships, that would be great.
Upvotes: 0
Views: 300
Reputation: 3703
Ok so if I got it right.
An earner is a family member so you can't do Earner hasOne FamilyMember
First of all, in simple words, a model is an implementation of a database table.
Since you don't have an earners table, then no need for a model called Earner
I have one question just to be clear: A beneficiary is a family member, and and earner is a family member as well, correct?
Then in my opinion your tables will be:
Families
Family_members
Beneficiaries
In Families
table:
A family will just have an id
and a name
In Family_members
table:
A family_member will have id
, name
, family_id
, is_earner
In Benficiaries
table:
A beneficiary will have id
, family_id
, family_member_id
So with that said, you can control that in Beneficiaries
table that there should be unique records for family_id
and family_member_id
combined, means that this table will hold each family's beneficiary only, and each family should have only 1 record in this table.
For earners, the boolean column is_earner
will indicate if a family_member
earns money to the family or not, with this, it will allow many earners per family (which makes sense)
Let me know if you have any further questions or if I got something wrong in your question!
Upvotes: 1