php - Laravel Collection 按星期几排序 MF
问题描述
我有一个按星期几分组的 Laravel 集合,但它们的顺序错误。我需要从星期一开始按星期几订购。
我的控制器:
public function getWeeklySchedule()
{
$testSchedule = VolunteerSchedule::all();
$result = $testSchedule->groupBy(['assignment', function ($item) {
return $item['scheduled_days'];
}]);
return $result;
}
结果如下所示:
"Wednesday": {
"kitchen": [
{
"id": 1,
"full_name": "Hipolito Carroll",
"created_at": "2021-11-05T17:33:52.000000Z",
"updated_at": "2021-11-05T17:33:52.000000Z"
}
],
"driver": [
{
"id": 3,
"full_name": "Prof. Conor Grimes I",
"created_at": "2021-11-05T17:33:52.000000Z",
"updated_at": "2021-11-05T17:33:52.000000Z"
}
],
"bagger": [
{
"id": 4,
"full_name": "Martine Kemmer Sr.",
"created_at": "2021-11-05T17:33:52.000000Z",
"updated_at": "2021-11-05T17:33:52.000000Z"
},
{
"id": 10,
"full_name": "Marcellus Walker",
"created_at": "2021-11-05T17:33:52.000000Z",
"updated_at": "2021-11-05T17:33:52.000000Z"
}
]
},
"Thursday": {
"kitchen": [
{
"id": 1,
"full_name": "Hipolito Carroll",
"created_at": "2021-11-05T17:33:52.000000Z",
"updated_at": "2021-11-05T17:33:52.000000Z"
},
{
"id": 7,
"full_name": "Leonardo Schaefer V",
"created_at": "2021-11-05T17:33:52.000000Z",
"updated_at": "2021-11-05T17:33:52.000000Z"
}
],
"driver": [
{
"id": 3,
"full_name": "Prof. Conor Grimes I",
"created_at": "2021-11-05T17:33:52.000000Z",
"updated_at": "2021-11-05T17:33:52.000000Z"
},
{
"id": 5,
"full_name": "Prof. Pablo Corwin",
"created_at": "2021-11-05T17:33:52.000000Z",
"updated_at": "2021-11-05T17:33:52.000000Z"
}
],
"bagger": [
{
"id": 6,
"full_name": "Samantha Feil",
"created_at": "2021-11-05T17:33:52.000000Z",
"updated_at": "2021-11-05T17:33:52.000000Z"
},
{
"id": 10,
"full_name": "Marcellus Walker",
"created_at": "2021-11-05T17:33:52.000000Z",
"updated_at": "2021-11-05T17:33:52.000000Z"
}
]
},
"Friday": {
"kitchen": [
{
"id": 1,
"full_name": "Hipolito Carroll",
"created_at": "2021-11-05T17:33:52.000000Z",
"updated_at": "2021-11-05T17:33:52.000000Z"
}
],
"driver": [
{
"id": 3,
"full_name": "Prof. Conor Grimes I",
"created_at": "2021-11-05T17:33:52.000000Z",
"updated_at": "2021-11-05T17:33:52.000000Z"
},
{
"id": 9,
"full_name": "Miss Eleonore Kutch IV",
"created_at": "2021-11-05T17:33:52.000000Z",
"updated_at": "2021-11-05T17:33:52.000000Z"
}
],
"bagger": [
{
"id": 4,
"full_name": "Martine Kemmer Sr.",
"created_at": "2021-11-05T17:33:52.000000Z",
"updated_at": "2021-11-05T17:33:52.000000Z"
},
{
"id": 10,
"full_name": "Marcellus Walker",
"created_at": "2021-11-05T17:33:52.000000Z",
"updated_at": "2021-11-05T17:33:52.000000Z"
}
]
},
"Tuesday": {
"kitchen": [
{
"id": 2,
"full_name": "Miss Lessie Torphy",
"created_at": "2021-11-05T17:33:52.000000Z",
"updated_at": "2021-11-05T17:33:52.000000Z"
},
{
"id": 7,
"full_name": "Leonardo Schaefer V",
"created_at": "2021-11-05T17:33:52.000000Z",
"updated_at": "2021-11-05T17:33:52.000000Z"
}
],
"bagger": [
{
"id": 10,
"full_name": "Marcellus Walker",
"created_at": "2021-11-05T17:33:52.000000Z",
"updated_at": "2021-11-05T17:33:52.000000Z"
}
]
},
"Saturday": {...
为简洁起见,某些字段已被删除。星期几以序列化数组的形式存储在数据库中。实现这一目标的最佳方法是什么?
解决方案
只需定义一个自定义排序函数,该函数使用一周中的几天的地图:
$days = ["Monday" => 1, "Tuesday" => 2, "Wednesday" => 3, "Thursday" => 4, "Friday" => 5, "Saturday" => 6, "Sunday" => 7];
$collection = collect(["Friday" => [], "Wednesday" => [], "Tuesday" => []]);
$collection->sortBy(fn($val, $key) => $days[$key]);
dump($collection);
输出:
=> Illuminate\Support\Collection {#4977
all: [
"Tuesday" => [],
"Wednesday" => [],
"Friday" => [],
],
}
所以你的代码变成:
public function getWeeklySchedule(): Collection
{
$days = ["Monday" => 1, "Tuesday" => 2, "Wednesday" => 3, "Thursday" => 4, "Friday" => 5, "Saturday" => 6, "Sunday" => 7];
return VolunteerSchedule::all()
->groupBy(['assignment', fn ($item) => $item['scheduled_days']])
->sortBy(fn ($val, $key) => $days[$key]);
}
推荐阅读
- python - 我正在尝试通过 python 中的 re.findall() 解析主题标签,但相邻的主题标签被排除在外
- kotlin - 错误:需要属性名称或接收器类型 - Kotlin
- php - 来自 Amazon Linux 2 (Elastic Beanstalk) 的 Laravel 工匠修补匠
- docker - 为 Docker 容器指定 ip 失败
- java - 如何在 JSP 或 Java 中将当前日期与数据库中的前一个日期进行比较?
- r - 如果 R stringr 中的两个字符之间没有标点符号,则构造匹配的模式
- vue.js - vue-router 应该在点击时路由,不工作
- python - pandas 按行值排序
- excel - VBA-如何复制值
- javascript - 转义 HTML 会导致安全问题吗?