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

チートシート

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

Description

新しい検証ルールを作成します。

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
    {
        //
    }
}

Stub

OptionStub file name
-rule.stub
implicitrule.implicit.stub

Source

10.x

framework/src/Illuminate/Foundation/Console/RuleMakeCommand.php at 10.x · laravel/framework
The Laravel Framework. Contribute to laravel/framework development by creating an account on GitHub.

11.x

framework/src/Illuminate/Foundation/Console/RuleMakeCommand.php at 11.x · laravel/framework
The Laravel Framework. Contribute to laravel/framework development by creating an account on GitHub.

コメント

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