Laravel 5.5电子邮件和密码字段是必需的

时间:2017-10-05 05:42:03

标签: laravel-5.5

我是新手并安装了Laravel 5.5并尝试使用登录功能。我正在使用自定义字段登录; user_name和user_password。当我提交我正在

  • 电子邮件字段是必填字段。
  • 密码字段是必需的。

但我的字段是用户表中的user_name和user_password

登录控制器

namespace App\Http\Controllers\Auth;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Session;

class LoginController extends Controller
{


use AuthenticatesUsers;


protected $redirectAfterLogout = '/';

/**
 * Create a new controller instance.
 *
 * @return void
 */
public function __construct()
{
    $this->middleware('guest', ['except' => 'logout']);
}

/**
 * Logout, Clear Session, and Return.
 *
 * @return void
 */
public function logout()
{
    $user = Auth::user();
    Log::info('User Logged Out. ', [$user]);
    Auth::logout();
    Session::flush();

    return redirect(property_exists($this, 'redirectAfterLogout') ? $this->redirectAfterLogout : '/');
}

protected function getCredentials(Request $request){
    return $request->only('user_name', 'user_password');
}


}

查看

@extends('layouts.app')

@section('content')
<div class="container">
<div>
  <a class="hiddenanchor" id="signup"></a>
  <a class="hiddenanchor" id="signin"></a>
  <div class="login_wrapper">
    <div class="animate form login_form">
      <section class="login_content">
        <form class="form-horizontal" role="form" method="POST" action="{{ route('login') }}">
          {{ csrf_field() }}
          <h1>Login Form</h1>
          <div>
            <input id="email" type="email" class="form-control" name="user_name" value="{{ old('user_name') }}" required autofocus>
          </div>
          <div>
            <input id="password" type="password" class="form-control" name="user_password" required>
          </div>
          <div>
            <button type="submit" name="login" class="btn btn-default submit">Log in</button>
            <a class="reset_pass" href="#">Lost your password?</a>
          </div>

          <div class="clearfix"></div>
          <div class="separator"></div>
        </form>
      </section>
    </div>
 </div>
</div>

2 个答案:

答案 0 :(得分:2)

您正在使用特征AuthenticatesUsers,它负责在用户提交表单后验证数据。如果你看一下这个文件,你会看到这个方法:

/**
 * Validate the user login request.
 *
 * @param  \Illuminate\Http\Request  $request
 * @return void
 */
protected function validateLogin(Request $request)
{
    $this->validate($request, [
        $this->username() => 'required|string',
        'password' => 'required|string',
    ]);
}

更具体地说,正如您所看到的,此方法负责在字段名称&#34;密码&#34;上调用验证器。并通过方法username()。如果您只想自定义用户名字段,则可以覆盖username()类中的LoginController方法:

public function username()
{
    return 'user_name';
}

但是由于您要自定义用户名字段名称和密码字段名称,我建议您覆盖validateLogin()类中的LoginController方法:

protected function validateLogin(Request $request)
{
    $this->validate($request, [
        'user_name' => 'required|string',
        'user_password' => 'required|string',
    ]);
}

希望有所帮助。

答案 1 :(得分:0)

您必须转到 app / User.php 并更改

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

protected $fillable = [
    'name', 'user_email', 'user_password',
];

它将解决您的问题。但我必须建议您将视图表单字段更改为电子邮件,密码而不是user_name,user_password