使用电子邮件登录或 laravel 6 中的 phone 无法正常工作

login with email or phone in laravel 6 is not working

我想在 laravel 身份验证中使用电子邮件或 phone 号码登录。我写了这些代码,但它 returns 我 email field is required

在User.php

protected $fillable = ['name', 'email', 'password', 'phone'];

在登录控制器

public function phone()
{
    $loginType = request()->input('phone');
    $this->phone = filter_var($loginType, FILTER_VALIDATE_EMAIL) ? 'email' : 'phone';
    request()->merge([$this->phone => $loginType]);

    return property_exists($this, 'phone') ? $this->phone : 'email';
}

login.blade.php

<div class="js-form-message form-group">
    <label class="form-label" for="phone">Email address or Phone
        <span class="text-danger">*</span>
    </label>
    <input type="text"
           class="form-control{{ $errors->has('email') || $errors->has('phone') ? ' is-invalid' : '' }}"
           name="phone"
           id="phone"
           placeholder="Email or Phone"
           value="{{ old('phone') }}">
    @error('email')
    <div class="invalid-feedback" style="display: block">
        {{ $message }}
    </div>
    @enderror
    @error('phone')
    <div class="invalid-feedback" style="display: block">
        {{ $message }}
    </div>
    @enderror
</div>

我现在该怎么办?你能帮忙吗?

您唯一需要做的就是告诉 Laravel 要检查的列:

在您的登录控制器中覆盖此方法:

public function username()
{
    return request()->filled('phone') ? 'phone' : 'email';
}

将方法 phone() 的名称更改为 username()。

public function username()
{
    $loginType = request()->input('phone');
    $this->phone = filter_var($loginType, FILTER_VALIDATE_EMAIL) ? 'email' : 'phone';
    request()->merge([$this->phone => $loginType]);

    return property_exists($this, 'phone') ? $this->phone : 'email';
}

现在对我有用。 谢谢大家的回答。