FatalThrowableError Laravel 5.2

时间:2017-08-07 12:40:05

标签: php laravel laravel-5.2

我正在尝试注册并在laravel 5.2中登录,但我正在接收

  

类型错误:传递给Illuminate \ Database \ Eloquent \ Model :: create()的参数1必须是数组类型,给定null,在/ Applications / XAMPP / xamppfiles / htdocs / blogs / app / Http / Controllers中调用第30行的/RegisterationController.php

这是我的注册控制器:

   <?php

    namespace App\Http\Controllers;

    use Illuminate\Http\Request;

    use App\Http\Requests;
    use App\User;

    class RegisterationController extends Controller
    {
        //

        public function create(){
            return view('sessions.create');
        }

        public function store(){

            //validate form

            $this->validate(request(),[
                'name' => 'required',
                'email' => 'required|email',
                'password' => 'required'
                ]);


            //store the data
            $user = User::create(request(['name','email','password']));

            //login
             auth()->login($user);

             //redirect

             return redirect()->home();
        }
    }

以下是用户模型:

namespace App;

use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name', 'email', 'password',
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];

    public function posts(){
        return $this->hasMany(Post::class);
    }

}

我正在关注laravel 5.4的视频,不确定这是不是问题。

1 个答案:

答案 0 :(得分:1)

您没有将数组传递给传递名为request()的函数结果的函数,该函数可能不存在。

所以修改这样的调用

//$user = User::create(request(['name','email','password']));
$user = User::create(
                    [ 
                        $request->input('name'),
                        $request->input('email'),
                        $request->input('password')
                    ]
                    );

$params = [ $request->input('name'),
            $request->input('email'),
            $request->input('password')
          ];
$user = User::create( $params );

User::create($request->only(['name','email','password']);