过滤多对多关系 - Laravel

Filter many-to-many relationsip - Laravel

我有一个约会网站,其数据库如下

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_forusergender 之间的支点 table。一个用户可以和多个性别约会。例如。对与男性和女性约会都有兴趣的女性。

userhasOneuser_profile

public function profileDetails(){
    return $this->hasOne('\App\UserProfile');
}

反之,user_profileuser

有 'belongsTo' 关系
public function user(){
    return $this->belongsTo('\App\User');
}

user_profilegenderbelongsTo 关系,因为每个用户个人资料都有性别

public function gender(){
    return $this->belongsTo('\App\Gender');
}

usergender 通过 looking_forbelongsToMany 因为用户可以将他的约会偏好设置为多个性别。

// 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 table,因为它更适合那里。

我也有兴趣table

interests (1-on-1 Dating, Online Friends, Swingers etc)
---------
id
name

usersinterests

之间的支点 table
interest_user
-------------
id
interest_id
user_id

因此,用户可以查找具有相似兴趣的多个性别。

比如说,一位女性希望与居住在美国的男性和女性约会,对 1-on-1 DatingOnline Friends

感兴趣

Laravel 很不错 collection filters ;)

尝试这样的事情:

$collection = Gender::with('lookingFor')->each(function($gender) {

  // get the lookingFor names
  $looking_for = $gender->lookingFor()->lists('name');

  // check if your case has been met
  if(in_array('Female', $looking_for)) return $gender;
});

注意,这是伪代码 ;)


Ps。如果您想了解有关 Laravel Collections 的更多信息,这里是我强烈推荐的 free ebook

@Gordon 的回答会奏效,但您基本上是从数据库中提取所有数据并迭代对象集合,在程序内存中挑剔您的答案,而不是利用专为此类问题构建的数据库引擎查询。 我会使用 laravel whereHas 方法来查询多对多关系

//assuming you are in the User instance you are trying to search for
$user = User::first();
$userGenderPref = $user->lookingFor()->get()->lists('gender_id');
$userInterestedIn = $user->interestedIn()->get()->lists('interest_id');
$results = User::whereHas('lookingFor', function($query) use ($userPref){//looking for gender
      $query->whereIn('gender_id',$userPref);
})
->whereHas('interestedIn', function($query) use ($userInterestedIn){//interested in
      $query->whereIn('interest_id',$userInterestedIn);
})
->where('user_id','!=',$user->user_id)//this is added to filter out our current user
->where('country_id',$user->country_id) //same country code as the original user
->get();

以上代码将 return 与您的用户具有相同偏好、性别和兴趣的用户列表,并且还将确保它与当前用户位于同一国家/地区。