Laravel-获取具有特定关系的用户

Laravel-获取具有特定关系的用户,laravel,eloquent,Laravel,Eloquent,我有一个用户与国家的关系。(有很多) 我想要的是让那些同时拥有两个国家(国家1和国家2)的用户知道我如何做到这一点?我正在读书,但我不知道该怎么办 编辑:几乎是解决方案 $users = \User::whereHas('countries', function($query) { $countries_ids = [1, 2, 4]; $query->whereIn('users_countries.country_id', $countries); })->get

我有一个用户与国家的关系。(有很多)

我想要的是让那些同时拥有两个国家(国家1和国家2)的用户知道我如何做到这一点?我正在读书,但我不知道该怎么办

编辑:几乎是解决方案

$users = \User::whereHas('countries', function($query) {
    $countries_ids = [1, 2, 4];
    $query->whereIn('users_countries.country_id', $countries);
})->get();

你可以用另一种方法。对国家/地区模型进行查询。在国家/地区模型中添加一个函数

 public function user(){
  return $this->belongsTo('user');//or if there is no user_id column in the countries table, use the ('model','local_key','foreign_key') pattern.
 }
然后创建一个带有country_id的数组,并创建country模型

 $country_ids=array(4,67);
 $countries=Country::where('id',$country_ids[0])->where('id',$country_ids[1])->get();
为了得到用户,你应该像这样循环

foreach($countries->user as $user)

你可以用另一种方法。对国家/地区模型进行查询。在国家/地区模型中添加一个函数

 public function user(){
  return $this->belongsTo('user');//or if there is no user_id column in the countries table, use the ('model','local_key','foreign_key') pattern.
 }
然后创建一个带有country_id的数组,并创建country模型

 $country_ids=array(4,67);
 $countries=Country::where('id',$country_ids[0])->where('id',$country_ids[1])->get();
为了得到用户,你应该像这样循环

foreach($countries->user as $user)

你可以用另一种方法。对国家/地区模型进行查询。在国家/地区模型中添加一个函数

 public function user(){
  return $this->belongsTo('user');//or if there is no user_id column in the countries table, use the ('model','local_key','foreign_key') pattern.
 }
然后创建一个带有country_id的数组,并创建country模型

 $country_ids=array(4,67);
 $countries=Country::where('id',$country_ids[0])->where('id',$country_ids[1])->get();
为了得到用户,你应该像这样循环

foreach($countries->user as $user)

你可以用另一种方法。对国家/地区模型进行查询。在国家/地区模型中添加一个函数

 public function user(){
  return $this->belongsTo('user');//or if there is no user_id column in the countries table, use the ('model','local_key','foreign_key') pattern.
 }
然后创建一个带有country_id的数组,并创建country模型

 $country_ids=array(4,67);
 $countries=Country::where('id',$country_ids[0])->where('id',$country_ids[1])->get();
为了得到用户,你应该像这样循环

foreach($countries->user as $user)

只要pivot表中没有重复项(例如,fk键对的唯一索引),这将起作用:


如果存在重复项,例如:

user_id | country_id
   1    |     1
   1    |     2
   1    |     1
然后您需要原始的
计数(不同的…
),因此您不能使用
whereHas
,因此我建议这样做,这样会更快:

$countries = [1,5,9];

User::join('users_countries as uc','uc.user_id', '=', 'users.id')
  ->whereIn('uc.country_id', $countries)
  ->having(DB::raw('count(distinct uc.country_id)'), '=', count($countries))
  ->groupBy('users.id')
  ->get(['users.*']);

只要pivot表中没有重复项(例如,fk键对的唯一索引),这将起作用:


如果存在重复项,例如:

user_id | country_id
   1    |     1
   1    |     2
   1    |     1
然后您需要原始的
计数(不同的…
),因此您不能使用
whereHas
,因此我建议这样做,这样会更快:

$countries = [1,5,9];

User::join('users_countries as uc','uc.user_id', '=', 'users.id')
  ->whereIn('uc.country_id', $countries)
  ->having(DB::raw('count(distinct uc.country_id)'), '=', count($countries))
  ->groupBy('users.id')
  ->get(['users.*']);

只要pivot表中没有重复项(例如,fk键对的唯一索引),这将起作用:


如果存在重复项,例如:

user_id | country_id
   1    |     1
   1    |     2
   1    |     1
然后您需要原始的
计数(不同的…
),因此您不能使用
whereHas
,因此我建议这样做,这样会更快:

$countries = [1,5,9];

User::join('users_countries as uc','uc.user_id', '=', 'users.id')
  ->whereIn('uc.country_id', $countries)
  ->having(DB::raw('count(distinct uc.country_id)'), '=', count($countries))
  ->groupBy('users.id')
  ->get(['users.*']);

只要pivot表中没有重复项(例如,fk键对的唯一索引),这将起作用:


如果存在重复项,例如:

user_id | country_id
   1    |     1
   1    |     2
   1    |     1
然后您需要原始的
计数(不同的…
),因此您不能使用
whereHas
,因此我建议这样做,这样会更快:

$countries = [1,5,9];

User::join('users_countries as uc','uc.user_id', '=', 'users.id')
  ->whereIn('uc.country_id', $countries)
  ->having(DB::raw('count(distinct uc.country_id)'), '=', count($countries))
  ->groupBy('users.id')
  ->get(['users.*']);


我不是100%确定结果,但您可以使用var_dump($countries)并查看您得到了什么。使用“where”方法,我将获得所有具有给定国家/地区之一的用户,我希望获得具有给定所有国家/地区的用户您是对的。请参阅我的编辑。您可以将where()链接起来。也就是说,我对结果不是100%肯定,但你可以把($countries)变为dump,看看你得到了什么。使用“WHERE”方法,我会得到所有拥有某个给定国家的用户,我希望得到拥有所有给定国家的用户。你是对的。请参阅我的编辑。您可以将where()链接起来。也就是说,我对结果不是100%肯定,但你可以把($countries)变为dump,看看你得到了什么。使用“WHERE”方法,我会得到所有拥有某个给定国家的用户,我希望得到拥有所有给定国家的用户。你是对的。请参阅我的编辑。您可以将where()链接起来。也就是说,我对结果不是100%肯定,但你可以把($countries)变为dump,看看你得到了什么。使用“WHERE”方法,我会得到所有拥有某个给定国家的用户,我希望得到拥有所有给定国家的用户。你是对的。请参阅我的编辑。您可以将where()链接起来。这就意味着什么东西和其他东西都很好!我只试了第一个,因为我没有重复的。谢谢,不客气。第二个解决方案在性能方面要好得多-没有依赖查询etc@JarekTkaczyk你能很好地帮我吗!我只试了第一个,因为我没有重复的。谢谢,不客气。第二个解决方案在性能方面要好得多-没有依赖查询etc@JarekTkaczyk你能很好地帮我吗!我只试了第一个,因为我没有重复的。谢谢,不客气。第二个解决方案在性能方面要好得多-没有依赖查询etc@JarekTkaczyk你能很好地帮我吗!我只试了第一个,因为我没有重复的。谢谢,不客气。第二个解决方案在性能方面要好得多-没有依赖查询etc@JarekTkaczyk你能帮我吗