简体   繁体   中英

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.

You shouldn't need to add the line Auth::login($user); in Laravel 5.4 for auto-login following registration. Replace:

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

with:

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());

        return $user;
}

If your Laravel framework and auth controllers code is up-to-date, the RegisterUsers trait should address it for you. This should also fix the issue you're having.

When use User::create

return User::create([
        'name' => $data['name'],
        'username' => $data['username'],
        'email' => $data['email'],
        'password' => bcrypt($data['password']),
    ]);

For default return $user

Solve to end return $user to end

$user = new User([
        'name' => $data['name'],
        'username' => $data['username'],
        'email' => $data['email'],
        'password' => bcrypt($data['password']),
    ]);
    $user->save();

    return $user;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM