Diego Utreras
Diego Utreras

Reputation: 111

Argument #1 ($user) must be of type Authenticable, null given

Im getting the following error in Laravel when trying to use the create function in RegisterController from Laravel auth. I'm trying to insert data into 2 tables with this function, into the Users table and a table called personas that is associated with the model Persona. The error is this

Illuminate\Auth\SessionGuard::login(): Argument #1 ($user) must be of type Illuminate\Contracts\Auth\Authenticatable, null given, called in D:\xampp\htdocs\SistemaHNF\vendor\laravel\ui\auth-backend\RegistersUsers.php on line 36

This is the code I have in the create function

 protected function create(array $data)
{

    $datos= ['nombre' => $data['name'],'apellido' => $data['surname'],'cedula' => $data['cedula'],'email' => $data['email'],
    'telefono' =>$data['telefono'],'direccion' =>$data['direccion'],'ciudadResi' =>$data['ciudadResi'],'genero' =>$data['genero'],]; 
    
    
    Persona::create([
        'nombre' => $datos['nombre'],
        'apellido' => $datos['apellido'],
        'cedula' => $datos['cedula'],
        'email' => $datos['email'],
        'telefono' =>$datos['telefono'],
        'direccion' =>$datos['direccion'],
        'ciudadResi' =>$datos['ciudadResi'],
        'fechaNacimiento' =>'1998-03-05',
        'genero' =>$datos['genero'],
        'estado'=> '1',
        'idTipoPersona'=>'2'
    ]);
    
    
    User::create([
        'name' => 'clienteUser',
        'surname' => $data['surname'],
        'email' => $data['email'],
        'nick' => $data['nick'],
        'password' => Hash::make($data['password']),
        'role' => 'cliente'
    ]);
    
}

Is it because I'm lacking a return in the function? And if I'm lacking it what should I return because I'm inserting data into 2 tables. I'm new to Laravel so sorry if this is a novice mistake, also english isn't my main language, if you need myself to explain better or any other code I can gladly provide it.

The form I'm using is the form from Register.Blade

Upvotes: 0

Views: 5347

Answers (1)

John Lobo
John Lobo

Reputation: 15319

Just return user instance back

protected function create(array $data)
{
    
        $datos= ['nombre' => $data['name'],'apellido' => $data['surname'],'cedula' => $data['cedula'],'email' => $data['email'],
        'telefono' =>$data['telefono'],'direccion' =>$data['direccion'],'ciudadResi' =>$data['ciudadResi'],'genero' =>$data['genero'],]; 
        
        
        Persona::create([
            'nombre' => $datos['nombre'],
            'apellido' => $datos['apellido'],
            'cedula' => $datos['cedula'],
            'email' => $datos['email'],
            'telefono' =>$datos['telefono'],
            'direccion' =>$datos['direccion'],
            'ciudadResi' =>$datos['ciudadResi'],
            'fechaNacimiento' =>'1998-03-05',
            'genero' =>$datos['genero'],
            'estado'=> '1',
            'idTipoPersona'=>'2'
        ]);
        
        
       $user=User::create([
            'name' => 'clienteUser',
            'surname' => $data['surname'],
            'email' => $data['email'],
            'nick' => $data['nick'],
            'password' => Hash::make($data['password']),
            'role' => 'cliente'
        ]);
    return $user;
        
}

As default method comment says, create method must return User model instance.

    /**
     * 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']),
        ]);
    }

Upvotes: 4

Related Questions