首页 > 解决方案 > 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": {...

为简洁起见,某些字段已被删除。星期几以序列化数组的形式存储在数据库中。实现这一目标的最佳方法是什么?

标签: phplaravellaravel-collection

解决方案


只需定义一个自定义排序函数,该函数使用一周中的几天的地图:

$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]);
}

推荐阅读