我具有以下控制器,允许用户使用laravel auth系统登录,但是当用户单击“登录”按钮时,出现以下错误:
传递给Illuminate \ Auth \ SessionGuard :: attempt()的参数1必须为数组类型,给定对象,在C:\ laragon \ www \ AGRIAPP \ projetinvestisseur \ AgriApp_Investor \ AgriAppInvestor \ vendor \ laravel \ framework中调用\ src \ Illuminate \ Foundation \ Auth \ AuthenticatesUsers.php,第82行
刷新页面时我登录,所以我想解决这个问题
根据文档,try函数需要一个数组和一个布尔值
LoginController.php
public function __construct()
{
$this->middleware('guest')->except('logout');
}
public function showLoginForm()
{
return view('auth.login');
}
protected function credentials(Request $request)
{
$credentials = array(
'slug' => $request->slug,
'password' => $request->password,
'statut' => 1,
);
if(Auth::attempt( $credentials,false ))
{
return Redirect::to( '/admin/home' );
}
}
public function username()
{
return 'slug';
}
protected function authenticated()
{
$user = auth()->user();
$user->online = true;
$user->save();
if ($user->rule->pluck( 'name' )->contains( 'abonne' )) {
return Redirect::to( '/admin-dashboard' );
}
return Redirect::to( '/admin/home' );
}
public function logout()
{
$user = Auth::user();
$user->online=false;
$user->save();
Auth::logout();
return redirect('/');
}}
AuthController.php
protected function attemptLogin(Request $request)
{
return $this->guard()->attempt(
$this->credentials($request), $request->filled('remember')
);
}
/**
* Get the needed authorization credentials from the request.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
protected function credentials(Request $request)
{
return $request->only($this->username(), 'password');
}
答案 0 :(得分:0)
您已经覆盖了credentials()
方法,但没有从中返回任何内容。
将您的凭据方法更改为:
protected function credentials(Request $request)
{
return [
'slug' => $request->slug,
'password' => $request->password,
'statut' => 1,
];
}
我意识到您正在尝试在凭据方法中对用户进行身份验证,但在这种情况下,您不需要这样做,因为调用它的方法也是如此。
您的重定向也不起作用的原因是因为调用方法没有返回它,因此您的用户已登录,但实际上是将重定向响应传递给attempt
方法,这是导致您的错误。
此外,我不确定statut
是否是错字?