LoginSignup
1
1

Requestクラスについて(Laravel)

Posted at

初めに

Requestクラスをよく使用するので、メモとして残します。

環境

開発環境 バージョン
Laravel 10

Requestファイルを作成する

php artisan make:request Hogehoge 

Requestクラス説明

HogehogeRequest.php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class Hogehoge extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     */
    public function authorize(): bool // ユーザーがこのリクエストを行う権限があるかどうかをチェックする。
    {
        return false; // trueでよく使う
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array|string>
     */
    public function rules(): array // バリデーションのルールを定義することができる。
    {
        return [
            // 例
            user_id = required|integer,
        ];
    }

    // バリデーションが成功した時に実行されるメソッド
    public function passedValidation(): void
    {
        // バリデーションが成功した時に実行したい処理
    }
}

Controllerでの使い方

HogehogeController.php

use App\Http\Requests\HogehogeRequest;

    /* 省略 */

    public function RequestTestMethod(HogehogeRequest $request)
    {
        Log::debug($request->all());
    }

最後に

最後まで閲覧いただきありがとうございました。
ご意見、ご指摘ありましたら、コメントお願いいたします。

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1