因为有一段时间我开始在Laravel为一个学校项目编程。现在我尝试实现登录系统。我的hometead虚拟机上运行了用户信息的数据库,并使用Postgresql运行。我已经迁移了表,因此它们存在且数据库正在运行。问题是:当我填写注册表并发送它时,我收到一个错误:
RegistersUsers.php第32行中的FatalErrorException:调用成员函数fail()on null
您可以在下面看到RegistersUsers.php的代码:
<?php
namespace Illuminate\Foundation\Auth;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
trait RegistersUsers
{
use RedirectsUsers;
/**
* Show the application registration form.
*
* @return \Illuminate\Http\Response
*/
public function getRegister()
{
return view('auth.register');
}
/**
* Handle a registration request for the application.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function postRegister(Request $request)
{
$validator = $this->validator($request->all());
if ($validator->fails()) {
$this->throwValidationException(
$request, $validator
);
}
Auth::login($this->create($request->all()));
return redirect($this->redirectPath());
}
}
当我尝试登录时会发出警告,数据库中不存在登录凭据(因为我还没有注册),所以似乎数据库连接正常工作。
有人知道这方面的解决方案吗?
---- ---- UPDATE
下面我添加了我的AuthController。您还可以在我的github上查看我的项目,以便在需要时查看我的其他文件:https://github.com/RobbieBakker/LaravelProject56
class AuthController extends Controller
{
/*
|--------------------------------------------------------------------------
| Registration & Login Controller
|--------------------------------------------------------------------------
|
| This controller handles the registration of new users, as well as the
| authentication of existing users. By default, this controller uses
| a simple trait to add these behaviors. Why don't you explore it?
|
*/
use AuthenticatesAndRegistersUsers, ThrottlesLogins;
private $redirectTo = '/';
/**
* Create a new authentication controller instance.
*
* @return void
*/
public function __construct()
{
$this->middleware('guest', ['except' => 'getLogout']);
}
/**
* Get a validator for an incoming registration request.
*
* @param array $data
* @return \Illuminate\Contracts\Validation\Validator
*/
protected function validator(array $data)
{
$validator = Validator::make($data, [
'name' => 'required|max:255',
'email' => 'required|email|max:255|unique:users',
'password' => 'required|confirmed|min:6',
]);
if(!$validator->fails())
{
session(['email' => $data['email']]);
}
}
/**
* Create a new user instance after a valid registration.
*
* @param array $data
* @return User
*/
protected function create(array $data)
{
return User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
]);
}
}
答案 0 :(得分:0)
我最终只是通过评论调用此错误的函数来解决它。我不确定它是否应该像这样解决,但我的登录系统现在似乎工作正常。
感谢您的支持!
答案 1 :(得分:0)
您忘记了验证程序函数返回验证程序实例))
protected function validator(array $data)
{
$validator = Validator::make($data, [
'name' => 'required|max:255',
'email' => 'required|email|max:255|unique:users',
'password' => 'required|confirmed|min:6',
]);
if(!$validator->fails())
{
session(['email' => $data['email']]);
}
return $validator;
}
我有同样的错误)
答案 2 :(得分:-1)
我认为你需要&#34;制作&#34;验证者。
尝试将其更改为此..
{{1}}
看看是否有效......