====== 【Laravel】バリデーションルールの追加(make:rule) ====== ファイルアップロート時にメガバイト(MB)単位でMaxサイズバリデーションを行う。 # バリデーションルールの追加 php artisan make:rule ImageFileMaxSize getSize() / 1024 / 1024; return $mega_bytes <= $this::MAX_MB; } /** * Get the validation error message. * * @return string */ public function message() { return ':attribute のサイズは' . $this::MAX_MB . 'MB以下にしてください。'; } } 追加したバリデーションルールの使い方 use App\Rules\ImageFileMaxSize; public function rules() { return [ 'file' => [ 'file', 'image', 'mimes:jpeg,jpg,png,gif', new ImageFileMaxSize, ], ]; }