我正在使用 Laravel 手动身份验证,并且需要区分大小写的用户名检查,但 Laravel 默认情况下不区分大小写检查,我在文档中找不到如何更改它。有没有一些简单的方法或者我需要编写自己的身份验证?
这是我的控制器
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
class AuthController extends Controller
{
/**
* Handle an authentication attempt.
*
* @param \Illuminate\Http\Request $request
*
* @return Response
*/
public function authenticate(Request $request)
{
$credentials = $request->only('username', 'password');
if (Auth::attempt($credentials)) {
return redirect()->intended('dashboard');
}
return redirect()->intended('login');
}
不区分大小写的匹配不是来自 Laravel 本身,而是来自您的数据库,该数据库(在大多数情况下)使用不区分大小写的排序规则来存储用户名。您可以将迁移更改为以下内容:
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('email')->unique()->collation('utf8_bin');
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
}
这会将电子邮件列与utf8_bin
不区分大小写的排序规则。然而,该集合将影响列的排序,因此任何带有ORDER BY email
如果您使用顺序不明确的 UTF8 字符,可能会返回不同的顺序。如果这是只能使用 ASCII 字符的电子邮件或用户名,则这不是问题。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)