Laravel 按星期几 M-F 排序的集合

Laravel Collection ordered by day of the week M-F

我有一个按星期几分组的 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": {...

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

只需定义一个 custom sort function,它使用地图表示星期几:

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