Laravel 5.4创建用户时出错

时间:2017-04-01 13:14:29

标签: php laravel laravel-5

我创建用户时遇到了问题。我收到了这个错误: SessionGuard.php第407行中的 FatalThrowableError:类型错误:传递给Illuminate \ Auth \ SessionGuard :: login()的参数1必须实现接口Illuminate \ Contracts \ Auth \ Authenticatable,null给定,在/ home / mariusz / Pulpit中调用第35行/www/szpital/vendor/laravel/framework/src/Illuminate/Foundation/Auth/RegistersUsers.php

用户是创建的但是这个错误太烦人了,我不知道怎么修复它。 我使用laravel auth和我编辑寄存器控制器因为我需要为用户添加角色,现在我的控制器看起来就是这样。

<?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);
        
        
    }
}
我不知道mybe我不应该编辑它。当我收到此错误时,我想解释一下: 1.我去页面创建acc。 我写名字等 3.当我点击创建我有错误 4.接下来,当我刷新页面用户创建时,登录并重定向到主页。

2 个答案:

答案 0 :(得分:1)

您不需要在Laravel 5.4中添加行Auth::login($user);以便在注册后自动登录。替换:

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

使用:

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

如果您的Laravel框架和身份验证控制器代码是最新的,RegisterUsers特征应该为您解决。这也应该解决你遇到的问题。

答案 1 :(得分:1)

使用User :: create

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

默认返回$ user

解决结束返回$ user结束

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

    return $user;