我正在使用不同版本的Laravel并且已经构建了一个使用Laravel 5.4的插件,其中不幸的是使用了引用Laravel 3的数据库 sic
“用户”表格中有一栏标题为“ user_email ”。
在重置密码上,我想更改数据库查询以检查“user_email”而不是“email”。因为我目前得到错误:
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'email' in 'where clause'
它正在尝试运行的SQL查询是:
SQL: select * from `users` where `email` = user@user.co.uk limit 1
有没有办法改变这个以适应?
由于
答案 0 :(得分:1)
要正确自定义Column email,您需要做一些更改,首先需要将该方法(getEmailForPasswordReset和routeNotificationFor)覆盖到模型User:
public function getEmailForPasswordReset()
{
return $this->email_user;
}
public function routeNotificationFor($driver)
{
if (method_exists($this, $method = 'routeNotificationFor'.Str::studly($driver))) {
return $this->{$method}();
}
switch ($driver) {
case 'database':
return $this->notifications();
case 'mail':
return $this->email_user;
case 'nexmo':
return $this->phone_number;
}
}
并实施:
use Illuminate\Support\Str;
进入ForgotPasswordController需要覆盖名为sendResetLinkEmail和validateEmail的方法:
public function sendResetLinkEmail(Request $request)
{
$this->validateEmail($request);
// We will send the password reset link to this user. Once we have attempted
// to send the link, we will examine the response then see the message we
// need to show to the user. Finally, we'll send out a proper response.
$response = $this->broker()->sendResetLink(
$request->only('email_user')
);
return $response == Password::RESET_LINK_SENT
? $this->sendResetLinkResponse($response)
: $this->sendResetLinkFailedResponse($request, $response);
}
protected function validateEmail(Request $request)
{
$this->validate($request, ['email_user' => 'required|email']);
}
你需要从PasswordBroker覆盖一些方法(validateNewPassword,validatePasswordWithDefaults,reset),但我们需要。
在App \ Providers中创建 CustomPasswordResetServiceProvider ,我在其中注册了 CustomPasswordBrokerManager 实例。
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use App\Services\CustomPasswordBrokerManager;
class CustomPasswordResetServiceProvider extends ServiceProvider{
protected $defer = true;
public function register()
{
$this->registerPasswordBrokerManager();
}
protected function registerPasswordBrokerManager()
{
$this->app->singleton('auth.password', function ($app) {
return new CustomPasswordBrokerManager($app);
});
}
public function provides()
{
return ['auth.password'];
}
}
在 config / app.php 中注明掉了一行:
//Illuminate\Auth\Passwords\PasswordResetServiceProvider::class,
并补充说:
App\Providers\CustomPasswordResetServiceProvider::class,
在App \ Services文件夹中创建了一个 CustomPasswordBrokerManager 并复制了位于以下位置的默认 PasswordBrokerManager 的上下文
照亮\验证\密码\ PasswordBrokerManager.php
然后修改功能解决以返回 CustomPasswordProvider 类的实例。
protected function resolve($name)
{
$config = $this->getConfig($name);
if (is_null($config)) {
throw new InvalidArgumentException("Password resetter [{$name}] is not defined.");
}
return new CustomPasswordBroker(
$this->createTokenRepository($config),
$this->app['auth']->createUserProvider($config['provider'])
);
}
最后,在App \ Services文件夹中,我创建了一个 CustomPasswordBroker 类,该类扩展了位于以下位置的默认 PasswordBroker : 照亮\ Auth \ Passwords \ PasswordBroker并覆盖我需要的功能。
use Illuminate\Auth\Passwords\PasswordBroker as BasePasswordBroker;
class CustomPasswordBroker extends BasePasswordBroker
{
// override the method validateNewPassword, validatePasswordWithDefaults and reset, you can take from Illuminate\Auth\Passwords\PasswordBroker
}
不确定这是否是最佳实施方式。
问候:)
答案 1 :(得分:0)
在ResetPasswordController
protected $username = 'user_email';
同时添加LoginController
以进行登录
答案 2 :(得分:0)
将此方法添加到LoginController
public function username()
{
return 'user_email';
}