Php 我怎样才能用雄辩的关系完成这个连接表? 目的
我想通过使用雄辩的关系而不是直接使用查询生成器来获得涉及单个用户的所有用户之间的共同兴趣的总和 表结构 用户Php 我怎样才能用雄辩的关系完成这个连接表? 目的,php,mysql,laravel,eloquent,relationships,Php,Mysql,Laravel,Eloquent,Relationships,我想通过使用雄辩的关系而不是直接使用查询生成器来获得涉及单个用户的所有用户之间的共同兴趣的总和 表结构 用户 +----+--------+ | id | name | +----+--------+ | 1 | John | | 2 | George | | 3 | Paul | +----+--------+ +----+-----------+ | id | name | +----+-----------+ | 1 | apple | | 2 |
+----+--------+
| id | name |
+----+--------+
| 1 | John |
| 2 | George |
| 3 | Paul |
+----+--------+
+----+-----------+
| id | name |
+----+-----------+
| 1 | apple |
| 2 | banana |
| 3 | pineapple |
+----+-----------+
+--------+------------+
| userId | interestId |
+--------+------------+
| 1 | 1 |
| 1 | 2 |
| 1 | 3 |
| 2 | 1 |
| 2 | 2 |
| 3 | 1 |
+--------+------------+
兴趣
+----+--------+
| id | name |
+----+--------+
| 1 | John |
| 2 | George |
| 3 | Paul |
+----+--------+
+----+-----------+
| id | name |
+----+-----------+
| 1 | apple |
| 2 | banana |
| 3 | pineapple |
+----+-----------+
+--------+------------+
| userId | interestId |
+--------+------------+
| 1 | 1 |
| 1 | 2 |
| 1 | 3 |
| 2 | 1 |
| 2 | 2 |
| 3 | 1 |
+--------+------------+
用户感兴趣
+----+--------+
| id | name |
+----+--------+
| 1 | John |
| 2 | George |
| 3 | Paul |
+----+--------+
+----+-----------+
| id | name |
+----+-----------+
| 1 | apple |
| 2 | banana |
| 3 | pineapple |
+----+-----------+
+--------+------------+
| userId | interestId |
+--------+------------+
| 1 | 1 |
| 1 | 2 |
| 1 | 3 |
| 2 | 1 |
| 2 | 2 |
| 3 | 1 |
+--------+------------+
例子
像这样的事
[
{
"id": 2,
"name": "George",
"mutualInterests": 2,
},
{
"id": 3,
"name": "Paul",
"mutualInterests": 1,
}
]
在您的用户模型中
public function interests()
{
return $this->belongsToMany('App\Interest');
}
你的兴趣模型
public function users()
{
return $this->belongsToMany('App\User');
}
现在进入控制器功能
$users = User::all();
现在在您的视图中,获取每个用户感兴趣的所有数量的计数
@foreach($users as $user)
{{ $user->interests()->count() }}
<!-- if you need to extract all interests then -->
@foreach($user->interests as $interest)
{{ $interest->name }}
@endforeach
@endforeach
@foreach($users作为$user)
{{$user->interests()->count()}
@foreach($user->利息为$interest)
{{$interest->name}
@endforeach
@endforeach
根据您的评论更新,以查看双方的利益
@foreach($users as $user)
{{ $user->interests()->count() }}
<!-- if you need to extract all interests then -->
<?php $mutualInterest= 0; ?>
@foreach($user->interests as $interest)
<?php
$check = checkfunction($interest->id);
if($check == TRUE){
$mutualInterest = $mutualInterest+1;
}
?>
{{ $interest->name }}
@endforeach
<?php echo "Mutual Intersts".$mutualInterest; ?>
@endforeach
@foreach($users作为$user)
{{$user->interests()->count()}
@foreach($user->利息为$interest)
{{$interest->name}
@endforeach
@endforeach
现在进入视图底部的检查功能
<?php
function checkfunction($id){
$interest = App\Interest::find($id);
if($interest->users()->count()>1){
return true;
}else{
return false;
}
}
?>
谢谢您的回答。在您的示例中,我将分别获得每个用户的兴趣总数。但我的建议是获得与单个用户相同的兴趣数量。例如,乔治和约翰有两个共同的兴趣爱好。我想知道乔治的详细情况以及他们共同感兴趣的总人数。@IsaacFerreira我编辑了答案。请再试一次