Skip to content
Advertisement

IlluminateAuthSessionGuard::login(): Argument #1 ($user) must be of type IlluminateContractsAuthAuthenticatable, null given, called in … line56

I am using fortify for creating and logging-in users. So as I add a new column ‘role’ in my database as well as in the model. I face this kind of error when signing-up a new user. Somehow, the user information will still be registered and stored in the database but an error is shown as I click Register. How do I fix this ? Below are my code.

Fortify, CreateNewUser.php

<?php

namespace AppActionsFortify;

use AppModelsUser;
use IlluminateSupportFacadesHash;
use IlluminateSupportFacadesValidator;
use LaravelFortifyContractsCreatesNewUsers;
use LaravelJetstreamJetstream;

class CreateNewUser implements CreatesNewUsers
{
    use PasswordValidationRules;

    /**
     * Validate and create a newly registered user.
     *
     * @param  array  $input
     * @return AppModelsUser
     */
    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',
        ]);
    }
}

user controller

<?php

namespace AppHttpControllers;

use AppModelsUser;
use IlluminateFoundationAuthUser as Authenticatable;
use IlluminateHttpRequest;

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

users table

<?php

use IlluminateDatabaseMigrationsMigration;
use IlluminateDatabaseSchemaBlueprint;
use IlluminateSupportFacadesSchema;

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');
    }
};

Advertisement

Answer

You are missing the return inside your function.

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

return $user;
7 People found this is helpful
Advertisement