【问题标题】:Laravel - Retrieve records from many to many relationshipLaravel - 从多对多关系中检索记录
【发布时间】:2019-04-15 14:39:36
【问题描述】:

我在多对多关系中有三个表

┌─────────────────┐          ┌─────────────────┐          ┌──────────────────┐
│ users           │          │ department_user │          │ departments      │
├─────────────────┤          ├─────────────────┤          ├──────────────────┤
| id              |          | id              |          | id               |
| name            |          | user_id         |          | name             |
└─────────────────┘          | department_id   |          └──────────────────┘
                             | joined_at       |          
                             └─────────────────┘          

部门表

1 | department 1
2 | department 2
3 | department 3

用户表

1| Mark
2| Jack

Department_user 表

Id | user_id | department_id | joined_at
1  |    1    |      1        | 2019-01-01 
2  |    2    |      1        | 2019-03-26
3  |    1    |      1        | 2019-04-01
4  |    1    |      3        | 2019-05-01

用户模型:

public function departments()
{
   return $this->belongsToMany('App\Department)->withPivot('joined_at');
}

部门模型:

public function users()
{
   return $this->belongsToMany('App\User')->withPivot('joined_at');
}

部门控制器:

$department = Department::with(['phones', 'users' => function($query)
{
  $query->orderBy('joined_at', 'desc');
}])->get();
return view('dashboard.departments.show', compact('department'));

Show.blade:

<h3>{{  department->name }}</h3><span>{{ $department->users->count() }}</span>
@foreach ($department->users as $user)
   <li>
      {{ $user-> name }} 
      {{ $user-> departments->last()->pivot->joined_at }}
   </li>
 @endforeach

我想要的是列出这样的用户

部门 1

1 users
Jack  –   joined_at 2019-03-26

部门 3

1 users
Mark   –   joined_at 2019-05-01

但我得到的结果是

部门 1

3 users
Mark   –   joined_at 2019-01-01
Jack  –   joined_at 2019-03-26
Mark   –   joined_at 2019-04-01

部门 3

1 users
Mark   –   joined_at 2019-05-01

所以我想列出某个部门的最后一个用户并计算他们的数量,但如果用户在以前的日期在该部门的位置,则忽略重复项

【问题讨论】:

    标签: php laravel


    【解决方案1】:

    如果您希望 多对多 关系没有重复,只需从 department_user 表中删除 Id 列,然后将表的主键设置为 @987654323 的组合@ 和department_id

    这将让您避免将来出现重复。

    【讨论】:

    • 稍后我将需要此副本以了解部门中的用户历史记录
    • 好的,你想知道最近的join_date,还是最早的join_date
    • 我想知道最近的join_date
    【解决方案2】:

    试试这个

    Department::join('department_user','department_user.department_id','=','departments.id')
    ->join('users', 'users.id', '=', 'department_user.user_id')
    ->selectRaw('users.id, COUNT(*) AS count, MAX(joined_at) AS joined_at')
    ->groupBy('department_user.user_id')
    ->having('count', '<', 2)  // ignore duplicates
    ->orderBy('joined_at', 'desc')
    ->get();
    

    【讨论】:

      【解决方案3】:
      $department = Department::with(['phones', 'users' => function($query)
      {
      $query->orderBy('joined_at', 'desc');
      }])->get();
      

      在你循环部门之前

      $department->users->groupBy('name');
      

      【讨论】:

      • SQLSTATE[42S22]:未找到列:1054 未知列 'users.name'
      猜你喜欢
      • 1970-01-01
      • 2021-08-21
      • 2016-10-28
      • 1970-01-01
      • 1970-01-01
      • 2017-05-25
      • 2019-08-17
      • 2017-01-17
      • 2023-03-12
      相关资源
      最近更新 更多