use Validator;
Validator::extend()を使う時はuse Validator;を呼び出すのがポイント
もくじ
環境
$ php artisan --version Laravel Framework 5.7.19
/var/www/vhosts/example.net/httpdocs/project/app/Providers/HelloServiceProvider.php
<?php namespace App\Providers; use Illuminate\Support\Facades\View; use Illuminate\Support\ServiceProvider; use Illuminate\Validation\Validator; use App\Http\Validators\HelloValidator; class HelloServiceProvider extends ServiceProvider { /** * Bootstrap services. * * @return void */ public function boot() { Validator::extend('hello', function($attribute, $value, $parameters, $validator){ return $value % 2 == 0; }); } }
Symfony \ Component \ Debug \ Exception \ FatalThrowableError (E_ERROR)
Call to undefined method Illuminate\Validation\Validator::extend()
エラー発生
/var/www/vhosts/example.net/httpdocs/project/app/Providers/HelloServiceProvider.php
<?php namespace App\Providers; use Illuminate\Support\Facades\View; use Illuminate\Support\ServiceProvider; //use Illuminate\Validation\Validator; ←無効化 use App\Http\Validators\HelloValidator; use Validator; ←追加 class HelloServiceProvider extends ServiceProvider { /** * Bootstrap services. * * @return void */ public function boot() { Validator::extend('hello', function($attribute, $value, $parameters, $validator){ return $value % 2 == 0; }); } }
表示出来た