Search code examples
phplaravellaravel-authentication

login with email or phone in laravel 6 is not working


I want to login with either email or phone number in laravel auth. I write these codes but it returns me email field is required

in User.php

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

in login Controller

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>

what should I do now? can you please help?


Solution

  • Changed the name of the method phone() to 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';
    }
    

    that is worked for me now. thanks everyone for your answer.