【もう迷わない】プログラミングスクールをポイント別に紹介

Laravel8でClass ‘App\Http\Requests\Rule’ not foundのエラー対応

バリデーションを実装しようとしてフォームリクエストファイルに、以下のようにRule::exsitsでIDの存在チェックをしたところClass ‘App\Http\Requests\Rule’ not foundが発生。

<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class BookRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        // falseからtrueに変更する
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        $validate = [];

        $validate += [
            'category_id' => [
                Rule::exists('categories')->where(function ($query) {
                    $query->where([
                        ['category_id', 1],
                    ]);
                })
            ]
        ];

        return $validate;
    }
}

どこでエラーが出ているか確認したところ、Ruleの部分でした。

Rule::exists('categories')->where(function ($query) {
// この一文が原因みたい
Contents

解決策

<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;←追加

class BookRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        // falseからtrueに変更する
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        $validate = [];

        $validate += [
            'category_id' => [
                Rule::exists('categories')->where(function ($query) {
                    $query->where([
                        ['category_id', 1],
                    ]);
                })
            ]
        ];

        return $validate;
    }
}

use Illuminate\Validation\Rule;を冒頭に追加することで解決できました。

ちなみに実務では実際にプルダウンで選択されたIDをチェックするので、バリデーションも以下のようになります。

<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Http\Request;←追加
use Illuminate\Validation\Rule;


class BookRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        // falseからtrueに変更する
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules(Request $request)
    {
        $validate = [];

        $validate += [
            'category_id' => [
                Rule::exists('categories')->where(function ($query) use ($request) {
                    $query->where([
                        ['category_id', $request->category_id],
                    ]);
                })
            ]
        ];

        $validate += [
            'book_name' => [
                'required',
                'max:10'
            ]
        ];

        return $validate;
    }
}

Rule::exists(‘categories’)->where(function ($query) use ($request)と$requestが使えるようにuse ($request)が追加されました。

これによって、public function rules()もpublic function rules(Request $request)に変更します。

今度はRequestがないぞー!ってエラーが出るので冒頭にuse Illuminate\Http\Request;を追加しましょう。

ちなみに「あれ?何を追加すればいいんだっけ??」ってなったら、configのapp.phpの中を見にいきましょう。

aliasesに色々とあります。ここを見て、例えばDBがないぞ!って言われたら’DB’の項目を見ます。

    'DB' => Illuminate\Support\Facades\DB::class

となっているので、useを追加、::classを削除すればいいのです。つまり、

use Illuminate\Support\Facades\DB;

をフォームリクエストやコントローラーに追加すればOKです。

    'aliases' => [

        'App' => Illuminate\Support\Facades\App::class,
        'Arr' => Illuminate\Support\Arr::class,
        'Artisan' => Illuminate\Support\Facades\Artisan::class,
        'Auth' => Illuminate\Support\Facades\Auth::class,
        'Blade' => Illuminate\Support\Facades\Blade::class,
        'Broadcast' => Illuminate\Support\Facades\Broadcast::class,
        'Bus' => Illuminate\Support\Facades\Bus::class,
        'Cache' => Illuminate\Support\Facades\Cache::class,
        'Config' => Illuminate\Support\Facades\Config::class,
        'Cookie' => Illuminate\Support\Facades\Cookie::class,
        'Crypt' => Illuminate\Support\Facades\Crypt::class,
        'Date' => Illuminate\Support\Facades\Date::class,
        'DB' => Illuminate\Support\Facades\DB::class,
        'Eloquent' => Illuminate\Database\Eloquent\Model::class,
        'Event' => Illuminate\Support\Facades\Event::class,
        'File' => Illuminate\Support\Facades\File::class,
        'Gate' => Illuminate\Support\Facades\Gate::class,
        'Hash' => Illuminate\Support\Facades\Hash::class,
        'Http' => Illuminate\Support\Facades\Http::class,
        'Lang' => Illuminate\Support\Facades\Lang::class,
        'Log' => Illuminate\Support\Facades\Log::class,
        'Mail' => Illuminate\Support\Facades\Mail::class,
        'Notification' => Illuminate\Support\Facades\Notification::class,
        'Password' => Illuminate\Support\Facades\Password::class,
        'Queue' => Illuminate\Support\Facades\Queue::class,
        'RateLimiter' => Illuminate\Support\Facades\RateLimiter::class,
        'Redirect' => Illuminate\Support\Facades\Redirect::class,
        // 'Redis' => Illuminate\Support\Facades\Redis::class,
        'Request' => Illuminate\Support\Facades\Request::class,
        'Response' => Illuminate\Support\Facades\Response::class,
        'Route' => Illuminate\Support\Facades\Route::class,
        'Schema' => Illuminate\Support\Facades\Schema::class,
        'Session' => Illuminate\Support\Facades\Session::class,
        'Storage' => Illuminate\Support\Facades\Storage::class,
        'Str' => Illuminate\Support\Str::class,
        'URL' => Illuminate\Support\Facades\URL::class,
        'Validator' => Illuminate\Support\Facades\Validator::class,
        'View' => Illuminate\Support\Facades\View::class,

    ],

コメント

コメントする

CAPTCHA


Contents
閉じる