php - Laravel 注册认证错误;怎么修?
问题描述
我在 Laravel 中的身份验证遇到了问题。运行基本身份验证脚手架后,我无法使用 Laravel 的基本登录和注册布局创建帐户。每次我填写姓名、电子邮件和密码并尝试创建新用户时,我都会收到以下错误:
Argument 1 passed to Illuminate\Auth\SessionGuard::login() must implement interface Illuminate\Contracts\Auth\Authenticatable, null given, called in /Applications/XAMPP/xamppfiles/htdocs/PRG05/websitemaria/vendor/laravel/ui/auth-backend/RegistersUsers.php on line 37
这是我目前正在使用的代码:
基本寄存器控制器
/**
* Create a new user instance after a valid registration.
*
* @param array $data
* @return \App\Models\User
*/
protected function create(array $data)
{
return User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => Hash::make($data['password']),
]);
}
路由,Auth::routes 已启用
//Get to the project
Route::get('/', [ProjectController::class, 'index'])->name('project');
//Enable Auth routes
Auth::routes();
//Route to function 'show' in AboutController
Route::get('/about', [AboutController::class, 'show'])->name('about');
//Route to function 'index' in HomeController
Route::get('/home', [App\Http\Controllers\HomeController::class, 'index'])->name('home');
//Route to the functions 'index' and 'upload' in CreateController
Route::get('create', [CreateController::class, 'index'])->name('create');
Route::post('create', [CreateController::class, 'store'])->name('store');
在数据库中创建用户表
class CreateUsersTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('email')->unique();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->string('image')->nullable();
$table->integer('role')->nullable();
$table->rememberToken();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('users');
}
}
用户.php
<?php
namespace App\Models;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Laravel\Sanctum\HasApiTokens;
class User extends Authenticatable
{
use HasApiTokens, HasFactory, Notifiable;
/**
* The attributes that are mass assignable.
*
* @var string[]
*/
protected $fillable = [
'name',
'email',
'password',
];
/**
* The attributes that should be hidden for serialization.
*
* @var array
*/
protected $hidden = [
'password',
'remember_token',
];
/**
* The attributes that should be cast.
*
* @var array
*/
protected $casts = [
'email_verified_at' => 'datetime',
];
public static function create(array $validate)
{
}
}
一些旁注:
- PHPstorm 似乎表明在 RegisterController 中找到的 create 函数正在返回 'void',
- 除了 CreateUsersTable 之外,我在搭建脚手架后没有更改任何身份验证文件,
- 我检查了我的 .ENV 设置,它们似乎是正确的,
- 奇怪的是,注册和登录过程在项目开始时有效,但最终停止工作。
解决方案
推荐阅读
- c - 地址值与定义的结构长度没有精确的差异。[C]
- flutter - 将'from'参数传递给动画控制器时闪烁动画?
- c++ - 如何在 C++ 中比较两个类型名是否相等?
- sql - 选择直到特定值的行
- gtk - GTK:当用户编辑某些文本字段时忽略/抑制键事件
- javascript - 在 wordpress 中使用 codepen 示例不起作用
- javascript - 当我关闭模式时,所有 hidden.bs.close 都是触发器
- mysql - 选择小时大于某个小时且日期为当天的所有字段(日期时间)
- javascript - D3 svg 组件不可调整大小
- xml - javax.xml.bind.JAXBException 在模块路径或类路径上找不到 JAXB-API 的实现