How to back-reference objects in self-referencing many-to-many relationship? - go

I am building a blog application and one of the features is being able to follow other users. This creates many-to-many relationship between the user which I declared like so:
type User struct {
gorm.Model
Username string
Password string
Followers []*User `gorm:"many2many:user_followers"`
}
When migrating this model, the following join table is created:
join table: user_friends
foreign key: user_id, reference: users.id
foreign key: follower_id, reference: users.id
My question is, how can I retrieve the followings of a user?
To get the followers of a user, I can simply do:
var followers []User
db.Model(&user).Association("Followers").Find(&followers)
But I can't figure out a way to retrieve the followings in a similar manner. I know I can query the join table to get the followings but this means that for each object thats returned from this query, I'd need another query to get the user associated with the user_id. This seems super inefficient at scale.
How can I do this effectively and efficiently?
Thank you!

Related

Third Table lookup on Eloquent

People Table:
id
name
Places Table:
id
name
Placeable Table:
people_id
places_id
relation_id
Relation Table:
id
label
In the people model, I can do:
return $this->morphToMany(Places::class, 'placeable')->withPivot('relation_id')
And I'll get the id number of the relationship in the pivot table. But, in my blade file, I'd like to put the label from the relationtable. "Manager" instead of "2", for example.
All the ways I've come up with so far create a lookup at every iteration in the blade. It seems like it would be great if I could append a "JOIN" to my eloquent in model to retrieve the label as it grabs everything else, but I'm not finding how to do that.

Are correlated sub queries supported by GraphQL

I am investigating the use of GraphQL
I have a use case which I cannot see how to achieve in a single GraphQL query.
I have the following data classes.
Type A Reference Data (Primary Key id)
Type A User Data (Foreign Key rd_id)
Type B User Data
I can execute three separate queries to retrieve all the necessary data
What I would like is to execute a single query and get all data
I can get Type A & B User data in one query fine.
However I also need the Type A Reference data returned with the associated Type A User Data.
Type A user data has a foreign key to the specific reference data row
I cannot see how I can write a GraphQL query that joins data from two separate types of data.
query experiment {
userA {
id
rd_id
name
address {
address_line_1
address_line_2
zip
}
}
userB {
id
business_name
trading_as
}
referenceData (id : $userA.rd_id) { // How can I achieve this join
type
type_name
limit
}
Does GraphQL allow me to join referenceData records to UserA type records like this in a "dynamic" way?

Laravel / Eloquent -- Lookup Table Issue

I have 3 tables that I am trying to work through and am having a hard time connecting them via Eloquent joins.
Character Table
profileID (PK)
Character Gear Table
profileId (PK)
qualityId (FK)
Quality Lookup Table
id (PK)
name
I am able to access the Character Gear Lookup with the following in my Character Model:
public function gear()
{
return $this->hasMany('App\Models\CharacterGear', 'profileId')->where('gearSet', '=', '0');
}
How do I get the lookup to work so that I can get the quality name from the Quality Lookup table to tie in to the gear() shown above?
Please let me know if you need any further information!
Figured it out. Eloquent has Nested Relationships and I didn't know that.

add relation that depends on the content of a table

So i have the following tables: 'order', 'comments', 'personnel' and 'contactperson'
What i want is to get all the orders with their comments and thats comment's author
so i do this to get the comments:
Order::with('comments')->get();
No problem here. But how can i get the author of those comments.
The Order table has the following fields:
id_author -> int
fk_author -> enum('personnel','contactperson')
Now depending on the fk_author field, the author should be fetched from the personnel table or the contactperson table.
How can i achieve this?
Look into polymorphic relations. On your orders table, you will want to rename your columns to fit what Eloquent expects author_id, author_type instead of id_author and fk_author

how to write parameter string for include for linq query?

If table Employee has foreign key for table person, department,
when get the entity of employee, I want to both associated entities loaded too.
how to write the linq query? like below?
var employee = this.Context.Employee.Include("Person, Department");
It seems not working.
var employee = this.Context.Employee.Include("Person").Include("Department");

Resources