我有一个约会网站,其数据库如下:
相反地,
在
country (list of all countries)
-------
id
name
users (It contains all types of users - Super Admin, Fronend user)
-----
id
role_id
name
email
password
country_id
active
user_profile(contains further details of frontend users)
------------
id
user_id
gender_id
photo
status
screenname
headline
location
sign
...and 12 more columns specific to frontend users
gender(specific to genders option - Male, Female, Gay, Prefer Not To Say, etc)
------
id
name
looking_for (pivot between user and gender)
-----------
id
user_id
gender_id
looking_for
是user
和gender
之间的数据透视表。一个用户可以约会多个性别。例如:一个女性对男性和女性都有兴趣。
user
与user_profile
之间是hasOne
关系。
public function profileDetails(){
return $this->hasOne('\App\UserProfile');
}
相反地,
user_profile
与user
具有“belongsTo”关系。public function user(){
return $this->belongsTo('\App\User');
}
user_profile
与 gender
之间具有 belongsTo
关系,因为每个用户资料都有性别信息
public function gender(){
return $this->belongsTo('\App\Gender');
}
user
和gender
通过looking_for
进行belongsToMany
关联,因为用户可以将约会偏好设置为多个性别。
// User
public function lookingFor(){
return $this->belongsToMany('\App\Gender', 'looking_for')->withTimestamps();
}
//Gender
public function lookedUpBy(){
return $this->belongsToMany('\App\User', 'looking_for')->withTimestamps();
}
现在,我正在尝试创建一个搜索功能。
例如,一个女性希望约会住在美国的男性和女性。还必须考虑那些将显示在结果中的男性和女性已将其looking_for
设为女性。
首先,我正在尝试获取所有将其looking_for
偏好设置为女性的用户。
Gender::with('lookedUpBy')->find($genderIdOfSearchedUser);
如何进一步筛选?
编辑
我已将gender_id
移动到users
表中,因为那里更适合它。
我还有一个兴趣表。
interests (1-on-1 Dating, Online Friends, Swingers etc)
---------
id
name
在
用户
和兴趣
之间建立一个数据透视表。interest_user
-------------
id
interest_id
user_id
因此,用户可以查找具有相似兴趣爱好的多个性别。
比如,一位女性正在寻找在美国生活、对“一对一约会”和“在线朋友”感兴趣的男性和女性。
interests
表。请查看更新的问题。 - Kanav