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
Related
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?
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 am stuck in situation where I need relation between 3 different tables. My tables are companies, products and Roles. Companies can assign multiple Roles to multiple products. The problem is Companies do not have any relationship with products. Products are added through admin.
Currently I have made a table company_product_role with structure company_id, product_id and role_id, the problem is how to make eloquent relation for insertion and retrieval. Either I am doing it correct or there is simple solution for it?
Any help will be appreciated.
I'm using Laravel 5.4 and have a model and table structure as follows:
users
-----
id
accounts
--------
id
holdings
--------
id
account_id (foreign key to account)
user_accounts
-------------
id
user_id
account_id
A user can have multiple accounts
An account can be shared by multiple users
Each account has multiple holdings
A user therefore indirectly has many holdings through their many accounts.
I need help to define a relation on the User model called "holdings" to get me all the holdings applicable to the user (based on the accounts they are linked to).
I've tried lots of different things and spent ages on google. I can get close with both belongsToMany and hasManyThrough, but they only seem to work for 3 table relationships where the intermediate table stores primary keys from the other tables. I can reduce my relationship to 3 tables (rather than 4) if I make use of the account_id foreign key on the holdings table to remove the need to join through the accounts table, however I can't seem to get this to work.
belongsToMany - holdings.id needs to be holdings.account_id:
select * from `holdings`
inner join `user_accounts` on `holdings`.`id` = `user_accounts`.`account_id`
where `user_accounts`.`user_id` = ?
hasManyThrough - user_accounts.id needs to be user_accounts.account_id:
select * from `holdings`
inner join `user_accounts` on `user_accounts`.`id` = `holdings`.`account_id`
where `user_accounts`.`user_id` = ?"
Well it's not strictly an answer as I was asking about laravel 5.4, but it is a possible solution.
It turns out I'm in luck as I came across this Laravel belongsToMany relationship defining local key on both tables which indicates the belongsToMany relationship has been enhanced in Laravel 5.5 to support this type of scenario.
I have upgraded my project to L5.5 and replaced my hack with the relation:
return $this->belongsToMany(Holding::class, 'user_accounts', 'user_id', 'account_id', null, 'account_id');
And am happy to say it seems to work perfectly - at least for basic gets which is my use case, I haven't tried any updates etc.
Thanks to #cyberfly and those who posted the answer above
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.