Laravel API中传递必填字段时,发布请求的一般错误

时间:2018-10-28 09:48:04

标签: post postman laravel-5.7

我有一个包含注册方法的REST API。我尝试在邮递员上测试此方法,但收到错误消息。

  

SQLSTATE [HY000]:常规错误:1364字段'password'没有默认值(SQL:插入users

password字段在user表中不能为空,因此我将其传递给请求。但是我不知道为什么邮递员会给我这个错误。 这是user表迁移文件:

 public function up()
{
    Schema::create('users', function (Blueprint $table) {
        $table->increments('id');
        $table->string('name');
        $table->string('family')->nullable();
        $table->string('username')->nullable();
        $table->string('phone')->nullable();
        $table->string('degree')->nullable();
        $table->integer('major_id')->nullable()->unsigned();
        $table->string('field')->nullable();
        $table->string('university')->nullable();
        $table->string('image')->nullable();
        $table->string('email')->unique();
        $table->timestamp('email_verified_at')->nullable();
        $table->string('password');
        $table->rememberToken();
        $table->timestamps();
        $table->foreign('major_id')->references('majors')->on('id')->onDelete('cascade');
    });
}

这是register函数:

 public function register(Request $request){

    $validator =  \Illuminate\Support\Facades\Validator::make($request->all(), [
        'name' => 'required|max:255',
        'email' => 'required|email|unique:users,email',
        'password' => 'required|min:6',
        'phone' => 'required',
    ]);

    if($validator->fails()){
        return $this->sendError('Validation Error.', $validator->errors());
    }

    $input = $request->all();
    $input['password']= Hash::make($input['password']);

    $user = User::create($input);

    $success= $user->createToken('MyApp')->accessToken;

    return $this->sendResponse($success, 'User register successfully.',$user->id);
}

这是register路线:

Route::post('register','API\Auth\AuthenticationController@register');

最后这是邮递员的输出: Postman image

0 个答案:

没有答案