【Laravel】artisanのmake:ruleコマンドを解説

Laravel

Laravel 10.xのartisanの「make:rule」コマンドを解説します。

Description

新しい検証ルールの作成

Usage

php artisan make:rule [-f|--force] [-i|--implicit] [--] <name>

Arguments

引数必須設営
name必須検証ルール名

Options

オプション省略形必須説明
forcef-ルールが既に存在する場合でも、クラスを作成
impliciti-暗黙のルールを生成

Examples

no options

php artisan make:rule Foo
   INFO  Rule [app/Rules/Foo.php] created successfully.  
<?php

namespace App\Rules;

use Closure;
use Illuminate\Contracts\Validation\ValidationRule;

class Foo implements ValidationRule
{
    /**
     * Run the validation rule.
     *
     * @param  \Closure(string): \Illuminate\Translation\PotentiallyTranslatedString  $fail
     */
    public function validate(string $attribute, mixed $value, Closure $fail): void
    {
        //
    }
}

implicit

php artisan make:rule --implicit Foo
   INFO  Rule [app/Rules/Foo.php] created successfully.  
<?php

namespace App\Rules;

use Closure;
use Illuminate\Contracts\Validation\ValidationRule;

class Foo implements ValidationRule
{
    /**
     * Indicates whether the rule should be implicit.
     *
     * @var bool
     */
    public $implicit = true;

    /**
     * Run the validation rule.
     *
     * @param  \Closure(string): \Illuminate\Translation\PotentiallyTranslatedString  $fail
     */
    public function validate(string $attribute, mixed $value, Closure $fail): void
    {
        //
    }
}

Follow me!

コメント

PAGE TOP
タイトルとURLをコピーしました