传递给 SessionGuard::login() 的参数 1 必须是 Laravel 5.2 中 Auth\Authenticatable 的实例
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38404448/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Argument 1 passed to SessionGuard::login() must be an instance of Auth\Authenticatable in Laravel 5.2
提问by Halnex
I am using Laravel 5.2.
我正在使用 Laravel 5.2。
I added a new field to the users
table called username
and I included it in the view and AuthController.php
我在users
名为的表中添加了一个新字段,并将其username
包含在视图中AuthController.php
I am also doing a check to see if the user is going to use a reserved slug for his username, if yes, I throw an error, if the username is good, I save in the database.
我也在做一个检查,看看用户是否要为他的用户名使用保留的 slug,如果是,我会抛出一个错误,如果用户名是好的,我会保存在数据库中。
I am checking against 2 reserved slugs banner
and image
- that's all I have reserved.
我正在检查 2 个保留的 slug banner
,image
这就是我保留的全部内容。
If I remove the check, the data gets saved in the database normally, including the reserved slugs. So I must do the check.
如果我删除检查,数据会正常保存在数据库中,包括保留的 slug。所以我必须做检查。
However, when I apply the check, I get this error
但是,当我申请支票时,我收到此错误
ErrorException in SessionGuard.php line 439:
Argument 1 passed to Illuminate\Auth\SessionGuard::login() must be an instance of Illuminate\Contracts\Auth\Authenticatable, string given, called in C:\xampp\htdocs\socialnet\vendor\laravel\framework\src\Illuminate\Foundation\Auth\RegistersUsers.php on line 63 and defined
SessionGuard.php 第 439 行中的 ErrorException:
传递给 Illuminate\Auth\SessionGuard::login() 的参数 1 必须是 Illuminate\Contracts\Auth\Authenticatable 的实例,给出的字符串,在 C:\xampp\htdocs\socialnet\vendor\laravel\framework\src\Illuminate 中调用\Foundation\Auth\RegistersUsers.php 在第 63 行并定义
This is my code in AuthController
after I've modified it
这是我AuthController
修改后的代码
protected function validator(array $data)
{
return Validator::make($data, [
'name' => 'required|max:255',
'email' => 'required|email|max:255|unique:users',
'password' => 'required|min:6|confirmed',
'username' => 'required|alpha_num|unique:users,username',
]);
}
protected function create(array $data)
{
$check = $this->slugCheck($data['username']);
if($check['status'] == 'nok') {
return 'this username is a reserved name';
} else {
return User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
'username' => $data['username'],
]);
}
}
public function slugCheck($slug){
$slug = strtolower($slug);
// is the slug in the banned words list
//
if(array_search($slug, array('','banner','image'))){
return array('status' => 'nok', 'message' => 'This slug is on a list of reserved names');
}
$user = User::where('username', '=', $slug)->first();
if($user){
return array('status' => 'nok', 'message' => 'This slug is already taken');
} else {
return array('status' => 'ok', 'message' => 'Slug is unique');
}
}
If I dd($check)
after declaring the $check
variable and choosing a reserved name, I get the correct response.
如果我dd($check)
在声明$check
变量并选择保留名称后,我会得到正确的响应。
array:2 [▼
"status" => "nok"
"message" => "This slug is on a list of reserved names"
]
回答by John Aldrin
That is because you're returning a string and the class is expecting an instance of Authenticatable.
那是因为您要返回一个字符串,而该类需要一个 Authenticatable 实例。
Moving your validation in a middleware is your option. Create a middleware that checks if the username is a 'Slug' and only procede to create() method when validation passes otherwise, return the string: 'The username is a reserved name'
在中间件中移动验证是您的选择。创建一个中间件来检查用户名是否为“Slug”,并且仅在验证通过时才继续执行 create() 方法,否则返回字符串:“用户名是保留名称”
Now you can clean up the AuthController by removing the slugCheck method and you can always certain that whenever create() method was called, that username isn't Slug.
现在,您可以通过删除 slugCheck 方法来清理 AuthController,并且您始终可以确定无论何时调用 create() 方法,该用户名都不是 Slug。
You can check the laravel documentation
你可以查看laravel文档