環境
Laravel10
Windows11
目的
Postmanでform-dataを使用し、Requestを送信。受け取った値を加工したい。
・string型をint型へ変換
・array配列の中身をint型へ変換
公式ドキュメント
prepareForValidation()
prepareForValidation()を使用することで、バリデーション前にデータを加工できる。
TestRequest.php
protected function prepareForValidation()
{
$this->merge([
'subject_id' => (int) $this->subject,
'class_ids' => array_map('intval', $this->class_ids),
'public_scope' => (bool) $this->public_scope
]);
}
参考記事