我有一个包含注册方法的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');