Saturday, April 1, 2017

Laravel 5.4 Error when create user

Hi i got problem when i create user. I got this error: FatalThrowableError in SessionGuard.php line 407: Type error: Argument 1 passed to Illuminate\Auth\SessionGuard::login() must implement interface Illuminate\Contracts\Auth\Authenticatable, null given, called in /home/mariusz/Pulpit/www/szpital/vendor/laravel/framework/src/Illuminate/Foundation/Auth/RegistersUsers.php on line 35

User is created anyway but this error is so anoying and i dont know how fix it. I use laravel auth and i little edit register controller becouse i need add role for user and now my controller looks like that.

<?php

namespace App\Http\Controllers\Auth;

use App\User;
use App\Role;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Validator;
use Illuminate\Foundation\Auth\RegistersUsers;
use Illuminate\Support\Facades\Auth;

class RegisterController extends Controller
{
    /*
    |--------------------------------------------------------------------------
    | Register Controller
    |--------------------------------------------------------------------------
    |
    | This controller handles the registration of new users as well as their
    | validation and creation. By default this controller uses a trait to
    | provide this functionality without requiring any additional code.
    |
    */

    use RegistersUsers;

    /**
     * Where to redirect users after registration.
     *
     * @var string
     */
    protected $redirectTo = '/home';

    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('guest');
    }

    /**
     * Get a validator for an incoming registration request.
     *
     * @param  array  $data
     * @return \Illuminate\Contracts\Validation\Validator
     */
    protected function validator(array $data)
    {
        return Validator::make($data, [
            'name' => 'required|max:255',
            'lastname' => 'required|max:255',
            'phonenumber' => 'required|max:255',
            'email' => 'required|email|max:255|unique:users',
            'password' => 'required|min:6|confirmed',
        ]);
    }


    /**
     * Create a new user instance after a valid registration.
     *
     * @param  array  $data
     * 
     */

     public function create(array $data)
    {
        $user = new User();
        $user->name = $data['name'];
        $user->lastname = $data['lastname'];
        $user->phonenumber = $data['phonenumber'];
        $user->email = $data['email'];
        $user->password = bcrypt($data['password']);
        $user->save();
        $user->roles()->attach(Role::where('name', 'User')->first());
        Auth::login($user);
        
        
    }
}
I dont know mybe i shouldnt edit it. I want explain when i got this error: 1.I go to page create acc. 2.I write name etc. 3.When i click create i got error 4. Next when i refresh page user is created,login and redirect to home page.

via Mariusz

Advertisement