SQLSTATE[HY000]: General error: 1364 Field 'username' doesn't have a default value
SQLSTATE[HY000]: General error: 1364 Field 'username' doesn't have a default value
你好,我尝试在 laravel 5.8 中创建注册,但是当我尝试注册时出现此错误:
tableuser.php
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('username')->unique();
$table->string('name');
$table->string('email')->unique();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->boolean('admin')->default(false);
$table->rememberToken();
$table->timestamps();
});
}
registe.blade.php
@extends('layouts.app')
@section('content')
<div class="container">
<div class="row justify-content-center">
<div class="col-md-8">
<div class="card">
<div class="card-header">{{ __('Register') }}</div>
<div class="card-body">
<form method="POST" action="{{ route('register') }}">
@csrf
<div class="form-group row">
<label for="username" class="col-md-4 col-form-label text-md-right">{{ __('Username') }}</label>
<div class="col-md-6">
<input id="username" type="text" class="form-control @error('username') is-invalid @enderror" name="username" value="{{ old('username') }}" required autocomplete="username" autofocus>
@error('username')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
@enderror
</div>
</div>
<div class="form-group row">
<label for="name" class="col-md-4 col-form-label text-md-right">{{ __('Name') }}</label>
<div class="col-md-6">
<input id="name" type="text" class="form-control @error('name') is-invalid @enderror" name="name" value="{{ old('name') }}" required autocomplete="name" autofocus>
@error('name')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
@enderror
</div>
</div>
<div class="form-group row">
<label for="email" class="col-md-4 col-form-label text-md-right">{{ __('E-Mail Address') }}</label>
<div class="col-md-6">
<input id="email" type="email" class="form-control @error('email') is-invalid @enderror" name="email" value="{{ old('email') }}" required autocomplete="email">
@error('email')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
@enderror
</div>
</div>
<div class="form-group row">
<label for="password" class="col-md-4 col-form-label text-md-right">{{ __('Password') }}</label>
<div class="col-md-6">
<input id="password" type="password" class="form-control @error('password') is-invalid @enderror" name="password" required autocomplete="new-password">
@error('password')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
@enderror
</div>
</div>
<div class="form-group row">
<label for="password-confirm" class="col-md-4 col-form-label text-md-right">{{ __('Confirm Password') }}</label>
<div class="col-md-6">
<input id="password-confirm" type="password" class="form-control" name="password_confirmation" required autocomplete="new-password">
</div>
</div>
<div class="form-group row mb-0">
<div class="col-md-6 offset-md-4">
<button type="submit" class="btn btn-primary">
{{ __('Register') }}
</button>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
@endsection
SQLSTATE[HY000]: General error: 1364 Field 'username' doesn't have a
default value (SQL: insert into users
(name
, email
, password
,
updated_at
, created_at
) values (denden73, denden73@gmail.com,
y$x.J7316.UH.UFnzZaeI5F.TTXkYyd.xVUlrjV6EoHA7J88R0X9Ode,
2019-07-25 12:50:54, 2019-07-25 12:50:54))
尝试将此添加到您的 User.php 模型文件
protected fillable = ['username', 'email', 'password'];
请确保您已遵守此规则。
1) 迁移时使用 nullable()。
-> 这是因为当您插入任何记录时,如果未设置字段值,则会发生此错误。
$table->string('username')->nullable();
2) 可在您的模型中填充。
-> 如果您没有将 nullable 设置为列,现在在插入记录时您尝试插入值,但如果该值在模型中不可填充,那么它不会插入,所以它会发生此错误。
protected $fillable = [
'username', 'name', 'email','password','admin'
];
3) 创建新用户时提及用户名。
-> 有时您在创建时错误地没有传递该值,因此如果该字段在 MySQL 中不为空,则会发生此错误。
$user = new App\User;
$user->username = 'xyz';
$user->save();
您的插入查询如下:
DB::table('users')->insert(array('username'=>$request->username,'name'=>$request->name,'email'=>$request->email,'password'=>$request->password));
你好,我尝试在 laravel 5.8 中创建注册,但是当我尝试注册时出现此错误:
tableuser.php
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('username')->unique();
$table->string('name');
$table->string('email')->unique();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->boolean('admin')->default(false);
$table->rememberToken();
$table->timestamps();
});
}
registe.blade.php
@extends('layouts.app')
@section('content')
<div class="container">
<div class="row justify-content-center">
<div class="col-md-8">
<div class="card">
<div class="card-header">{{ __('Register') }}</div>
<div class="card-body">
<form method="POST" action="{{ route('register') }}">
@csrf
<div class="form-group row">
<label for="username" class="col-md-4 col-form-label text-md-right">{{ __('Username') }}</label>
<div class="col-md-6">
<input id="username" type="text" class="form-control @error('username') is-invalid @enderror" name="username" value="{{ old('username') }}" required autocomplete="username" autofocus>
@error('username')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
@enderror
</div>
</div>
<div class="form-group row">
<label for="name" class="col-md-4 col-form-label text-md-right">{{ __('Name') }}</label>
<div class="col-md-6">
<input id="name" type="text" class="form-control @error('name') is-invalid @enderror" name="name" value="{{ old('name') }}" required autocomplete="name" autofocus>
@error('name')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
@enderror
</div>
</div>
<div class="form-group row">
<label for="email" class="col-md-4 col-form-label text-md-right">{{ __('E-Mail Address') }}</label>
<div class="col-md-6">
<input id="email" type="email" class="form-control @error('email') is-invalid @enderror" name="email" value="{{ old('email') }}" required autocomplete="email">
@error('email')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
@enderror
</div>
</div>
<div class="form-group row">
<label for="password" class="col-md-4 col-form-label text-md-right">{{ __('Password') }}</label>
<div class="col-md-6">
<input id="password" type="password" class="form-control @error('password') is-invalid @enderror" name="password" required autocomplete="new-password">
@error('password')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
@enderror
</div>
</div>
<div class="form-group row">
<label for="password-confirm" class="col-md-4 col-form-label text-md-right">{{ __('Confirm Password') }}</label>
<div class="col-md-6">
<input id="password-confirm" type="password" class="form-control" name="password_confirmation" required autocomplete="new-password">
</div>
</div>
<div class="form-group row mb-0">
<div class="col-md-6 offset-md-4">
<button type="submit" class="btn btn-primary">
{{ __('Register') }}
</button>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
@endsection
SQLSTATE[HY000]: General error: 1364 Field 'username' doesn't have a default value (SQL: insert into
users
(name
,password
,updated_at
,created_at
) values (denden73, denden73@gmail.com, y$x.J7316.UH.UFnzZaeI5F.TTXkYyd.xVUlrjV6EoHA7J88R0X9Ode, 2019-07-25 12:50:54, 2019-07-25 12:50:54))
尝试将此添加到您的 User.php 模型文件
protected fillable = ['username', 'email', 'password'];
请确保您已遵守此规则。
1) 迁移时使用 nullable()。
-> 这是因为当您插入任何记录时,如果未设置字段值,则会发生此错误。
$table->string('username')->nullable();
2) 可在您的模型中填充。
-> 如果您没有将 nullable 设置为列,现在在插入记录时您尝试插入值,但如果该值在模型中不可填充,那么它不会插入,所以它会发生此错误。
protected $fillable = [
'username', 'name', 'email','password','admin'
];
3) 创建新用户时提及用户名。
-> 有时您在创建时错误地没有传递该值,因此如果该字段在 MySQL 中不为空,则会发生此错误。
$user = new App\User;
$user->username = 'xyz';
$user->save();
您的插入查询如下:
DB::table('users')->insert(array('username'=>$request->username,'name'=>$request->name,'email'=>$request->email,'password'=>$request->password));