You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Laravel Fortify新增role字段后注册报错:SessionGuard::login参数为null

问题解决:Laravel Fortify注册时SessionGuard::login参数错误

错误信息

Illuminate\Auth\SessionGuard::login(): 参数 #1 ($user) 必须为 Illuminate\Contracts\Auth\Authenticatable 类型,传入了 null,调用位置:...第56行

问题场景

使用Laravel Fortify实现用户创建与登录功能,在数据库及User模型中新增role字段后,注册新用户时触发上述错误。用户信息已成功存入数据库,但点击“注册”按钮时弹出错误提示。


相关代码

Fortify的CreateNewUser.php

<?php

namespace App\Actions\Fortify;

use App\Models\User;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Validator;
use Laravel\Fortify\Contracts\CreatesNewUsers;
use Laravel\Jetstream\Jetstream;

class CreateNewUser implements CreatesNewUsers
{
    use PasswordValidationRules;

    /**
     * Validate and create a newly registered user.
     *
     * @param  array  $input
     * @return \App\Models\User
     */
    public function create(array $input)
    {
        Validator::make($input, [
            'name' => ['required', 'string', 'max:255'],
            'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
            'password' => $this->passwordRules(),
            'terms' => Jetstream::hasTermsAndPrivacyPolicyFeature() ? ['accepted', 'required'] : '',
        ])->validate();

        $user = User::create([
            'name' => $input['name'],
            'email' => $input['email'],
            'password' => Hash::make($input['password']),
            'role' => 'user',
        ]);
    }
}

UserController.php

<?php

namespace App\Http\Controllers;

use App\Models\User;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Http\Request;

class UserController extends Controller
{
    public function index()
    {
        $user = User::get();
        return view('users.index', compact('user'));
    }
}

用户表迁移文件

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('users', function (Blueprint $table) {
            $table->id();
            $table->string('staff_id')->nullable();
            $table->string('name');
            $table->string('email')->unique();
            $table->string('department')->nullable();
            $table->timestamp('email_verified_at')->nullable();
            $table->string('password');
            $table->string('role');
            $table->rememberToken();
            $table->foreignId('current_team_id')->nullable();
            $table->string('profile_photo_path', 2048)->nullable();
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('users');
    }
};

问题原因与修复方法

核心原因

CreateNewUser类的create方法未返回创建好的$user实例。Fortify在完成用户创建后,会依赖该方法的返回值执行登录操作,当前返回null,导致SessionGuard::login接收到非法参数抛出错误。

修复步骤

  1. 修改CreateNewUser.php,在方法末尾添加返回用户实例的语句:
public function create(array $input)
{
    // 原验证与用户创建代码...

    $user = User::create([
        'name' => $input['name'],
        'email' => $input['email'],
        'password' => Hash::make($input['password']),
        'role' => 'user',
    ]);

    // 添加此行返回用户实例
    return $user;
}
  1. 确认User模型配置:确保User模型已继承Illuminate\Foundation\Auth\User类(该类实现了Authenticatable接口),同时将role字段加入$fillable数组(符合批量赋值最佳实践):
<?php

namespace App\Models;

use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
    protected $fillable = [
        'name',
        'email',
        'password',
        'role', // 新增role字段
    ];

    // 其他模型代码...
}

内容的提问来源于stack exchange,提问作者jabs93

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.14 08:30:35