I have a user table. Now, a user can be either a lender or a borrower. Both of them can have multiple loans. Now I want to create a relation between these three tables.
Here's what I want.
# A user can be a lender or borrower
# Both lenders and borrowers can have multiple loans.
# One borrower can have one loan only (typical one-to-one relationship).
# One loan can belong to multiple lenders. (later I want to distribute the loan).
I also tried polymorphic relations but those belong to only one table. But I want a single loan to be for one borrower and multiple lenders.
Now, How do I do this in laravel eloquent?
Related
i have two pivot tables based on three models- user, client and location
location_user-
user_id
location_id
client_location-
client_id
location_id
i want to get clients according to locations defined by user.i have defined the relationship as belongs To Many.how can i achieve it.thanks
Using Eloquent, I'm trying to make a polymorphic relationship between some models that isn't working out. Simplified, it works like this: there is a model 'groups', which obviously can contain some groups. Now each of these groups can either contain 'members' or 'guests', so I have a table and model for both of them. These are not similar to each other. My groups table contains a column in which model to use (members or guests). The structure is as follows:
groups (\App\Models\Group)
id
name
model (either \App\Models\Member or \App\Models\Guest)
members (\App\Models\Member)
id
group_id
first_name
last_name
address
email
.....
guests (\App\Models\Guest)
id
group_id
name
So the relationship 'people' in a group retrieves either the members or the guests. Using the default $this->morphTo() function using the group_id is able to retrieve the correct results, but due to its logic only returns one result, while I need all results. The rest of the app is indifferent to whether the people are members or guests, so I do not want to split those two up here.
Any ideas on how to accomplish this? I'm trying to eager load the relationship.
I'm working in a data set that tracks ownership of firms in a database. I have three databases. One with all the firms, one with all unique owners and one database with the relations between the owners and the firms. Each owner can own multiple firms. I want a calculated column that for each firm shows how many firms the specific owner or owners has. I have no idea how to do it.
It is something like; for each firm, search how many times the owner or owners appears in the relationship database and sum those to give a number for each firm.
After all data is in data model and relationships are created, calculated column in table Firms should look like this:
CountFirmsOfOwner = COUNTROWS(FILTER(Firms, Firms[OwnerID] = EARLIER(Firms[OwnerID])))
I have a table customers with the fields id, name and so on.
One table doctors with the fields id, name.
Then there is one table subject_areas which has all subject areas which a doctor can have. The fields are id, text.
So, each doctor can have multiple subject areas. There is one pivot table doctor_subject which is a belongsToMany relation.
Here is my problem: A customer can have multiple doctors, but only for a specific subject area. I tried it with a new table customer_doctor with the fields id, customer_id and doctor_subject_id. But how do i map this in Eloquent?
Issue was in relation between tables. After chat clarification this came out as solution:
Html form is written in a way that customer first choose doctor, then depending on selection choose several of his available areas.
In that scenario customer needn't to be related to areas directly and should be related to areas only over relation with doctor.
Also as side note, if needed deeper relations, models on pivot tables could be created and used as well.
SQL Server 2012 MVC3 EF4.3.1 Code First project.
I have a Teacher and Student table with a one to many relationship. The Teacher’s tables Id will be used as the account number so its Id numbering needs to be separate from the Student’s. I would like to create a Person table (containing shared properties such as First, Last, Phone, Email) to reduce redundancy on the properties. Person will also have a one to many relationship to an Address table.
I’ve thought of trying a Table per Hierarchy model with Teacher and Student inheriting from Person but then the Id sets would not be separate and I would have to have a one to many relationship internally on the Person table. I could generate the ID’s through code but is an internal one to many doable or practical?
Another scenario would be to setup Person as a child table with a one to one between and Teacher and Person and a one to one between Student and Person but I’m not sure how or if it’s possible to have two separate one to one’s on a table.
Is there a practical way to do what I want or should I not worry about the redundancy and not use a Person table? If I went that route would it be possible to have two separate one to many relationships to an Address table (Teacher-Address and Student-Address)? Or for that matter a one to many (Teacher-Address, teacher may have an additional shipping address) and one to one (Student-Address)?
Thank you
Another way to do it is to have a one to one between a Person and a Role table. Teacher and Student are merely roles in this arrangement. A given Role can be fulfilled by many Person instances.
You could also do a Person table with an IsTeacher flag.
I can see two possibilities:
One: Go with your Student and Teacher inheriting from a base table of Person and not worry about the 'redundancy'. It's not a redundancy because your relating a Student and a Teacher not a Person to a Person and so in your database and DOM the Person table and Person class know nothing of the Teacher to Student relationship, it only knows that its a person. The teacher and student relationships are stored in there respective types, not the person type. Also, look at Table per Type instead of Table per Heiarchy. It's much cleaner and crisper looking in the database and you don't get all the information of each type in the heiarchy in one table.
Two: Create a table that specifically holds information that both Students and Teachers share and have that related to both the Student and Teacher table separately. You could call it something like "ContactInformation".
Being a teacher and being a student are roles of people, not types of people.
You should have a table for People, a table TeachCourse to say that a Person is the teacher of a course (which in some cases are multiple teachers), a table AssistCourse to say which persons are attending a class as a student. You might have people that teach a course and assist another course, and that wasn't properly modeled in your first version.
You can also create a ContactInformation or ShippingInformation table for People to specify all their data (Some people may have multiple phones, or emails to).