![](/img/trans.png)
[英]How to use SQL Server database login cridentials as user login in laravel
[英]User login does not work in laravel with sql server. previously created database
我正在开发一个 sql server 上的数据库已经存在的项目,所以我必须更改表和列的名称才能运行用户注册表。 注册工作正常,现在,登录不起作用。 尝试登录时,页面什么也不做,只是重新加载。 如果你能帮助我,我将不胜感激,谢谢
------------User model--------------
<?php
namespace App;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
class User extends Authenticatable
{
protected $table = 'tb_postulantes';
protected $primaryKey = 'id_postulante';
use Notifiable;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'nombre_postulante', 'appaterno_postulante', 'correo_postulante', 'password_postulante',
'apmaterno_postulante', 'telefono_postulante', 'fechaingreso_postulante', 'estado_postulante'
];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'password_postulante', 'remember_token',
];
/**
* The attributes that should be cast to native types.
*
* @var array
*/
protected $casts = [
'email_verified_at' => 'datetime',
];
}
-------RegisterController----------
<?php
namespace App\Http\Controllers\Auth;
use App\Http\Controllers\Controller;
use App\User;
use Illuminate\Foundation\Auth\RegistersUsers;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Validator;
class RegisterController extends Controller
{
/*
|--------------------------------------------------------------------------
| Register Controller
|--------------------------------------------------------------------------
|
| This controller handles the registration of new users as well as their
| validation and creation. By default this controller uses a trait to
| provide this functionality without requiring any additional code.
|
*/
use RegistersUsers;
/**
* Where to redirect users after registration.
*
* @var string
*/
protected $redirectTo = '/';
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
$this->middleware('guest');
}
/**
* Get a validator for an incoming registration request.
*
* @param array $data
* @return \Illuminate\Contracts\Validation\Validator
*/
protected function validator(array $data)
{
return Validator::make($data, [
'nombre_postulante' => ['required', 'string', 'max:255'],
'correo_postulante' => ['required', 'string', 'email', 'max:255', 'unique:tb_postulantes,correo_postulante'],
'password' => ['required', 'string', 'min:8', 'confirmed'],
]);
}
/**
* Create a new user instance after a valid registration.
*
* @param array $data
* @return \App\User
*/
protected function create(array $data)
{
return User::create([
'nombre_postulante' => $data['nombre_postulante'],
'correo_postulante' => $data['correo_postulante'],
'password_postulante' => Hash::make($data['password']),
'appaterno_postulante' => $data['nombre_postulante'],
'apmaterno_postulante' => $data['nombre_postulante'],
'telefono_postulante' => $data['nombre_postulante'],
'fechaingreso_postulante' => '2012-02-21T18:10:00',
'estado_postulante' => '1',
]);
}
}
----------LoginController--------
<?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';
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
$this->middleware('guest')->except('logout');
}
public function beforelogin()
{
return view('Auth.beforelogin');
}
}
首先,Laravel 身份验证使用字段“电子邮件”来登录用户 - 您的用户模型中没有此字段,因此如果您想更改它,您必须在 LoginController 中定义用户名,如下所示:
public function username()
{
return 'correo_postulante';
}
对于不同的密码字段,您可以在您的用户模型中使用访问器,如下所示:
public function getAuthPassword()
{
return $this->password_postulante;
}
您可以在 Laravel 文档https://laravel.com/docs/5.5/authentication#included-authenticating 中找到更多信息
但是,如果您想使用所有三个参数来登录用户,我建议您使用不同的逻辑制作自己的 LoginController。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.