我有一个laravel项目需要使用multi-auth,所以我通过添加一个'u'前缀来更改users表的列名。对于注册,这一切都很好,但每当我尝试登录什么都没有发生,我得到重定向回/登录没有错误或任何东西。这是我的
登录名:login.blade.php
@extends('layouts.app')
@section('content')
<div class="container">
<div class="row">
<div class="col-md-8 col-md-offset-2">
<div class="panel panel-default">
<div class="panel-heading">Login</div>
<div class="panel-body">
<form class="form-horizontal" method="POST" action="{{ route('login') }}">
{{ csrf_field() }}
<div class="form-group{{ $errors->has('email') ? ' has-error' : '' }}">
<label for="email" class="col-md-4 control-label">E-Mail Address</label>
<div class="col-md-6">
<input id="email" type="email" class="form-control" name="email" value="{{ old('email') }}" required autofocus>
@if ($errors->has('email'))
<span class="help-block">
<strong>{{ $errors->first('email') }}</strong>
</span>
@endif
</div>
</div>
<div class="form-group{{ $errors->has('password') ? ' has-error' : '' }}">
<label for="password" class="col-md-4 control-label">Password</label>
<div class="col-md-6">
<input id="password" type="password" class="form-control" name="password" required>
@if ($errors->has('password'))
<span class="help-block">
<strong>{{ $errors->first('password') }}</strong>
</span>
@endif
</div>
</div>
<div class="form-group">
<div class="col-md-6 col-md-offset-4">
<div class="checkbox">
<label>
<input type="checkbox" name="remember" {{ old('remember') ? 'checked' : '' }}> Remember Me
</label>
</div>
</div>
</div>
<div class="form-group">
<div class="col-md-8 col-md-offset-4">
<button type="submit" class="btn btn-primary">
Login
</button>
<a class="btn btn-link" href="{{ route('password.request') }}">
Forgot Your Password?
</a>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
@endsection
user.php模型
<?php
namespace App;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable
{
use Notifiable;
protected $primaryKey = "uid";
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'u_username','u_password','u_email','u_first_name','u_last_name','u_country','u_status','u_type',
];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'password', 'remember_token',
];
}
以及
登录控制器.php
<?php
namespace App\Http\Controllers\Auth;
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';
public function username()
{
return 'u_email';
}
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
$this->middleware('guest')->except('logout');
}
}
看起来login.blade.php并不关心我是否更改了 function username() return value
或者没有,除了上面的可填充数组之外,我什么都没有改变。
1条答案
按热度按时间s5a0g9ez1#
将修改后的字段添加到可填充数组中并修改username函数是不够的。您必须重写验证器并在registercontroller类中创建方法。请参阅laravel文档:
验证/存储定制
要修改新用户向应用程序注册时所需的表单字段,或者自定义新用户存储到数据库中的方式,可以修改registercontroller类。此类负责验证和创建应用程序的新用户。
registercontroller的validator方法包含应用程序新用户的验证规则。您可以随意修改此方法。
registercontroller的create方法负责使用雄辩的orm在数据库中创建新的app\user记录。您可以根据数据库的需要自由修改此方法。
https://laravel.com/docs/5.6/authentication?#included-验证
我希望这有帮助。