首先,談下語言包的問題
1、安裝語言包,通過composer進行安裝composer require "overtrue/laravel-lang:dev-master"
'Illuminate\Translation\TranslationServiceProvider'改為 'Overtrue\LaravelLang\TranslationServiceProvider',
[ "numeric" => "密碼 至少 :min.", "string" => "密碼至少 :min 位.", ], "confirmed" => "密碼兩次輸入不一致", ];
[ "numeric" => "密碼 至少 :min.", "string" => "密碼至少 :min 位.", ], "confirmed" => "密碼兩次輸入不一致", ];
use App\Services\Registrar; 例如我要創建一個 function createUser() { $data = array( 'name' => 'admins', 'email' => '[email protected]', 'password' => '12345' ); $register = new Registrar; $validator = $register->validator($data); //通過Registrar類中的驗證方法驗證輸入信息 if(!$validator->fails()) { $result = $register->create($data); if($result) { return array('code' => 1, 'info' => '用戶創建成功'); } } else { $messages = $validator->messages(); return array('code' => 0, 'info'=> $messages->first()); } } Registrar類的寫法如下,幾乎為默認沒有什麼改動 'required|max:255', 'email' => 'required|email|max:255|unique:users', 'password' => 'required|min:6', ]); return $validator; } /** * Create a new user instance after a valid registration. * * @param array $data * @return User */ public function create(array $data) { return User::create([ 'name' => $data['name'], 'email' => $data['email'], 'password' => bcrypt($data['password']), ]); } }