Describe the feature
I need to check values through relationships. for example, i have a user model that has a pet model using a belongsTo relation. i now want to get a list of all users that have a pet with the breed_type "dog" for example. how would i do this in gorm efficiently? or is this feature still missing.
Motivation
I always used the whereHas function from laravel (https://stackoverflow.com/questions/30231862/laravel-eloquent-has-with-wherehas-what-do-they-mean)
Related Issues
Comment From: vicleos
whereHas, has, whereExisit ... all need ~ XD
Comment From: github-actions[bot]
This issue has been automatically marked as stale because it has been open 60 days with no activity. Remove stale label or comment or this will be closed in 30 days
Comment From: genofire
i am search also for a whereHas version with Preload / Joins ... lets tag a look xD
Comment From: github-actions[bot]
This issue has been automatically marked as stale because it has been open 60 days with no activity. Remove stale label or comment or this will be closed in 30 days
Comment From: renaldiadrian98
Any update on whereHas solution for gorm?
Comment From: genofire
@jinzhu ??
Comment From: a67793581
所以到底有没有解决方案
Comment From: jnatsa
use Joins Preloading gorm v1.25.2
type User struct {
ID uint64 gorm:"column:id;primary_key;AUTO_INCREMENT"
Username string gorm:"type:varchar(100);not null;index:idx_users_username,unique"
Password string json:"-" gorm:"type:varchar(255);not null"
Posts *[]Post gorm:"PRELOAD:false;foreignKey:AuthorID;references:ID;"
}
type Post struct {
ID uint64 gorm:"column:id;primary_key;AUTO_INCREMENT"
Title string gorm:"column:title;NOT NULL;type:varchar(255)"
Slug string gorm:"column:slug;NOT NULL;type:varchar(255)"
content string gorm:"column:content;type:longtext"
CreatedAt time.Time gorm:"column:created_at"
UpdatedAt time.Time gorm:"column:updated_at"
AuthorID uint64 gorm:"column:author_id"
Author User gorm:"PRELOAD:false;foreignKey:AuthorID;constraint:OnUpdate:CASCADE,OnDelete:SET NULL"
}
users User
db.join("join post on user.id = post.user_id and post.created_at > ?", filterCreatedAtValue).Preload("Posts").Find(&users)