问题描述
我有2个表和1个数据透视表,将它们用作历史记录"表,"dateChange"列指示该员工所在的商店,因为我使用的是 Orderby('dateChange','DESC').一切正常,但是如何在控制器中过滤结果?没有REPEAT记录?我需要显示属于一个唯一商店的所有员工.我再次重复:我使用'dateChange'因为我知道最后一家商店.请帮助我.谢谢
I have 2 table and 1 pivot table wich I use as table Historical, the column 'dateChange' indicated the store where is the employee, because i use Orderby ('dateChange','DESC'). All is ok, however how can I FILTER the results in my controller? without REPEAT records?.I need to show all the employees that belong to a unique store. I repeat again: I use 'dateChange' for i know the last store. help me, please. Thanks
数据透视表(员工商店)
Pivot Table (employee_store)
- FK_idStore
- FK_idEmployee
- dateChange
表员工
- idEmployee
- 名称
表存储
- idStore
- nameStore
- 方向
型号
public function employee(){
return $this->belongsToMany(employee::class,'employee_store', 'fk_idStore','fk_idEmployee')
->withPivot('dateChange')->orderBy('dateChange','DESC');
控制器
$store= Store::findOrFail($id)->employee
return $store
推荐答案
要获取每个商店中每个员工的最新记录,请使用:
To get the last record of each employee in each store, use:
public function employee(){
return Employe_Store::all()->groupBy('fk_idEmployee')->max('dateChange');
}
这篇关于选择数据,在数据透视表中不重复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!