Daftar dan Belajar Sekarang!

Setting login form laravel dengan user id atau email tetapi error mewajibkan menggunakan email saja

foto MuhammadRamdani
7 Kontribusi 1 Coto
2 minggu yang lalu
Saat saya setting login form laravel dengan menggunakan User id atau email yang terjadi ialah diwajibkan mengisi email, padahal sudah saya setting dapat login juga dengan cara input user id, proses registrasi beres dan saat login dengan email juga sukses tapi disayangkan login dengan user id gagal. petunjuk saya menggunakan: sumber coding berikut contoh login.blade.php yang saya sesuaikan dengan kebutuhan saya
<div class="form-group row">
                            <label for="login" class="col-sm-4 col-form-label text-md-right">
                                {{ __('User_ID or Email') }}
                            </label>
                         
                            <div class="col-md-6">
                                <input id="login" type="text"
                                       class="form-control{{ $errors->has('user_id') || $errors->has('email') ? ' is-invalid' : '' }}"
                                       name="login" value="{{ old('user_id') ?: old('email') }}" required autofocus>
                         
                                @if ($errors->has('user_id') || $errors->has('email'))
                                    <span class="invalid-feedback">
                                        <strong>{{ $errors->first('user_id') ?: $errors->first('email') }}</strong>
                                    </span>
                                @endif
                            </div>
                        </div>
dan berikut logincontroller.php
<?php
 
namespace App\Http\Controllers\Auth;
 
use Illuminate\Http\Request; 
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
 
class LoginController extends Controller
{
    /*
    |--------------------------------------------------------------------------
    | Login Controller
    |--------------------------------------------------------------------------
    |
    | This controller handles authenticating users for the application and
    | redirecting them to your home screen. The controller uses a trait
    | to conveniently provide its functionality to your applications.
    |
    */
 
    use AuthenticatesUsers;
 
    /**
     * Where to redirect users after login.
     *
     * @var string
     */
    protected $redirectTo = '/home';
 
    /**
     * Login username to be used by the controller.
     *
     * @var string
     */
    protected $user_id;
 
    /**
     * Create a new controller instance.
     */
    public function __construct()
    {
        $this->middleware('guest')->except('logout');
 
        $this->user_id = $this->findUserid();
    }
 
    /**
     * Get the login username to be used by the controller.
     *
     * @return string
     */
    public function findUserid()
    {
        $login = request()->input('login');
 
        $fieldType = filter_var($login, FILTER_VALIDATE_EMAIL) ? 'email' : 'user_id';
 
        request()->merge([$fieldType => $login]);
 
        return $fieldType;
    }
 
    /**
     * Get username property.
     *
     * @return string
     */
    public function user_id()
    {
        return $this->user_id;
    }
}
https://i.imgur.com/XyjipsD.png jika dirasa ada yg bisa membantu saya dalam pemecahannya saya sangat terbantu sekali dan terima kasih sebanyak2nya

Jawaban
foto mrizkiromdoni
28 Kontribusi 6 Coto
1 minggu yang lalu
cara gampangnya dan lebih bersih dengan meng-override method credentials. Contoh kode-nya seperti ini
<?php
 
namespace App\Http\Controllers\Auth;
 
use Illuminate\Http\Request; 
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
 
class LoginController extends Controller
{
    /*
    |--------------------------------------------------------------------------
    | Login Controller
    |--------------------------------------------------------------------------
    |
    | This controller handles authenticating users for the application and
    | redirecting them to your home screen. The controller uses a trait
    | to conveniently provide its functionality to your applications.
    |
    */
 
    use AuthenticatesUsers;
 
    /**
     * Where to redirect users after login.
     *
     * @var string
     */
    protected $redirectTo = '/home';
 
    /**
     * Login username to be used by the controller.
     *
     * @var string
     */
    protected $user_id;
 
    /**
     * Create a new controller instance.
     */
    public function __construct()
    {
        $this->middleware('guest')->except('logout'); 
    }
 
    /**
     * Get the login username to be used by the controller.
     *
     * @return string
     */
 
   protected function credentials(Request $request)
{
    if(is_numeric($request->get('user_id'))){
        return ['user_id'=>$request->get('user_id'),'password'=>$request->get('password')];
    }
    return $request->only($this->username(), 'password');
}
}


Login untuk diksusi di forum sekolah koding