英文:
Laravel login on mobile without password: "Column not found: 1054 Unknown column 'password' in 'field list'"
问题
以下是您要翻译的内容:
当我登录时,我收到这个错误。
LoginController.php
public function login(Request $request)
{
    $credentials = $request->only('mobile');
    if (Auth::attempt($credentials)) {
        // Authentication passed...
        return redirect()->route('home');
    }
    // User doesn't exist, create a new one
    $user = User::create($credentials);
    Auth::login($user);
    return redirect()->route('home');
}
这个错误发生在以下代码行:
if (Auth::attempt($credentials)) {
以及
我在注册表单和登录表单中都有一个字段,即“mobile”(无密码)。
我该如何设计表单,以便如果用户表数据库中存在手机号码,就会登录。如果不存在,就创建它。
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'password' in 'field list'
public function login(Request $request)
{
    $credentials = $request->only('mobile');
    $credentials['password'] = 'default_password';
    if (Auth::attempt($credentials)) {
        // Authentication passed...
        return redirect()->route('home');
    }
    // User doesn't exist, create a new one
    $user = User::create($credentials);
    Auth::login($user);
    return redirect()->route('home');
}
我想要仅通过手机号码登录,无需密码。
英文:
When I login, I get this error.
LoginController.php
public function login(Request $request)
{
    $credentials = $request->only('mobile');
    if (Auth::attempt($credentials)) {
        // Authentication passed...
        return redirect()->route('home');
    }
    // User doesn't exist, create a new one
    $user = User::create($credentials);
    Auth::login($user);
    return redirect()->route('home');       
}
This error occurred at the following line:
if (Auth::attempt($credentials)) {
And
I have a field, the mobile, (and without password) in both the registration form and the login form.
How can I design the form in such a way that if the mobile number is present in the user table database, it will login. If it doesn't exist, create it.
> SQLSTATE[42S22]: Column not found: 1054 Unknown column 'password' in 'field list'
public function login(Request $request)
{
    $credentials = $request->only('mobile');
    $credentials['password'] = 'default_password';
    if (Auth::attempt($credentials)) {
        // Authentication passed...
        return redirect()->route('home');
    }
    // User doesn't exist, create a new one
    $user = User::create($credentials);
    Auth::login($user);
    return redirect()->route('home');
}
I want to login without a password only on mobile.
答案1
得分: 2
Laravel 期望一个名为 password 的字段用于其 Auth::attempt() 方法,当你执行 $user = User::create($credentials); 时,如果你的 users 表中没有一个密码字段,将会抛出错误。
你可以使用其他方式来登录用户,比如 Auth::login() 或 Auth::loginUsingId() 来绕过这个问题。
假设你的用户表看起来像这样:
+------------+
| users      |
+------------+
| id (pk)    |
| mobile     |
| timestamps |
+------------+
public function login(Request $request)
{
    $mobile = $request->mobile;
    $user = User::firstOrCreate(['mobile' => $mobile]);
    Auth::login($user);
    return redirect()->route('home');
}
你甚至可以通过在请求上使用 validate() 方法来添加一些验证。
public function login(Request $request)
{
    $data = $request->validate(['mobile' => ['required']]);
    $user = User::firstOrCreate($data);
    Auth::login($user);
    return redirect()->route('home');
}
如果你使用了 @error Blade 指令,这将抛出一个 ValidationException,应该可以在视图上很好地呈现。
英文:
Laravel expects a field named password for it's Auth::attempt() method and when you do $user = User::create($credentials);, if your users table does not have a password field you will get an error thrown.
You could use another way to login your user such as Auth::login() or Auth::loginUsingId() to bypass that.
Assuming your user table looks like this:
+------------+
| users      |
+------------+
| id (pk)    |
| mobile     |
| timestamps |
+------------+
public function login(Request $request)
{
    $mobile = $request->mobile;
    $user = User::firstOrCreate(['mobile' => $mobile]);
    Auth::login($user);
    return redirect()->route('home');
}
You can even add a bit of validation by using the validate() method on the request.
public function login(Request $request)
{
    $data = $request->validate(['mobile' => ['required']]);
    $user = User::firstOrCreate($data);
    Auth::login($user);
    return redirect()->route('home');
}
This will throw a ValidationException that should render nicely on the view if you're using the @error blade directive
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论