首页 > 解决方案 > 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)
    {
    }
}


一些旁注:

标签: phplaravelauthenticationregistration

解决方案


推荐阅读