ID 列默认值错误尽管自动增量
ID column default value error despite autoincrement
我是 laravel 的新手,我正在尝试创建用户数据库。但是我收到关于我的 ID
列的错误消息,即使我将它设置为自动增量。
这是我的迁移
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
use Illuminate\Support\Facades\DB;
return new class extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->increments('ID');
$table->string('FirstName');
$table->string('LastName');
$table->string('Email')->unique();
$table->string('Password');
$table->timestamps();
});
DB::statement('ALTER TABLE `users` MODIFY `ID` INT(10) ZEROFILL;');
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('users');
}
};
我还没有在模型中做任何事情,所以它仍然是默认的
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Users extends Model
{
use HasFactory;
}
这是我的POST方法
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Users;
use Illuminate\Support\Facades\Hash;
class UserAuthController extends Controller
{
public function registerUser(Request $request) {
$request -> validate([
'firstname' => 'required',
'lastname' => 'required',
'email' => 'required|email|unique:users,Email|confirmed',
'password' => 'required|min:8|max:25|confirmed'
]);
$user = new Users();
$user -> firstname = $request -> firstname;
$user -> lastname = $request -> lastname;
$user -> email = $request -> email;
$user -> password = Hash::make($request -> password);
$res = $user -> save();
if($res) {
return back() -> with('success', 'You are now registered!');
}
else {
return back() -> with('failed', 'Registration failed!');
}
}
}
这是我的注册表单
<div class="container form__container">
<form action="{{ route('auth.user.reguser') }}" method="POST">
@if (Session::has('success'))
<div class="alert alert-success">{{ Session::get('success') }}</div>
@endif
@if (Session::has('fail'))
<div class="alert alert-success">{{ Session::get('fail') }}</div>
@endif
@csrf
<h1 class="form-title">SIGN UP</h1>
<div class="field__container">
<div class="field field-name">
<input type="text" name="firstname" id="firstname" class="form-input input-firstname" placeholder="First Name" value="{{ old('firstname') }}" /><input type="text" name="lastname" id="lastname" class="form-input input-lastname" placeholder="Last Name" value="{{ old('lastname') }}" />
</div>
<div class="field">
<input type="email" name="email" id="email" class="form-input input-email" placeholder="Email" value="{{ old('email') }}"" />
</div>
<div class="field">
<input type="email" name="email_confirmation" id="email_confirmation" class="form-input input-email_confirmation" placeholder="Re-enter Email" />
</div>
<div class="field">
<input type="password" name="password" id="password" class="form-input input-password" placeholder="Password" />
</div>
<div class="field">
<input type="password" name="password_confirmation" id="password_confirmation" class="form-input input-password_confirmation" placeholder="Re-enter Password" />
</div>
</div>
<div class="button__container">
<button type="submit" name="btn_signup" class="btn-signup">Register</button>
<span>Already have an account? <a href="{{ route('auth.user.login') }}" class="btn-login">Login here.</a></span>
</div>
</form>
</div>
您的 DB::Statement
应该改为:
alter table `users` modify `id` int(10) unsigned zerofill not null auto_increment;
不要 删除 auto_increment
标志。您正在将其放入 alter
.
记住 zerofill
是 mysql 8 中已弃用的功能。它可能会在未来版本中删除。
我是 laravel 的新手,我正在尝试创建用户数据库。但是我收到关于我的 ID
列的错误消息,即使我将它设置为自动增量。
这是我的迁移
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
use Illuminate\Support\Facades\DB;
return new class extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->increments('ID');
$table->string('FirstName');
$table->string('LastName');
$table->string('Email')->unique();
$table->string('Password');
$table->timestamps();
});
DB::statement('ALTER TABLE `users` MODIFY `ID` INT(10) ZEROFILL;');
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('users');
}
};
我还没有在模型中做任何事情,所以它仍然是默认的
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Users extends Model
{
use HasFactory;
}
这是我的POST方法
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Users;
use Illuminate\Support\Facades\Hash;
class UserAuthController extends Controller
{
public function registerUser(Request $request) {
$request -> validate([
'firstname' => 'required',
'lastname' => 'required',
'email' => 'required|email|unique:users,Email|confirmed',
'password' => 'required|min:8|max:25|confirmed'
]);
$user = new Users();
$user -> firstname = $request -> firstname;
$user -> lastname = $request -> lastname;
$user -> email = $request -> email;
$user -> password = Hash::make($request -> password);
$res = $user -> save();
if($res) {
return back() -> with('success', 'You are now registered!');
}
else {
return back() -> with('failed', 'Registration failed!');
}
}
}
这是我的注册表单
<div class="container form__container">
<form action="{{ route('auth.user.reguser') }}" method="POST">
@if (Session::has('success'))
<div class="alert alert-success">{{ Session::get('success') }}</div>
@endif
@if (Session::has('fail'))
<div class="alert alert-success">{{ Session::get('fail') }}</div>
@endif
@csrf
<h1 class="form-title">SIGN UP</h1>
<div class="field__container">
<div class="field field-name">
<input type="text" name="firstname" id="firstname" class="form-input input-firstname" placeholder="First Name" value="{{ old('firstname') }}" /><input type="text" name="lastname" id="lastname" class="form-input input-lastname" placeholder="Last Name" value="{{ old('lastname') }}" />
</div>
<div class="field">
<input type="email" name="email" id="email" class="form-input input-email" placeholder="Email" value="{{ old('email') }}"" />
</div>
<div class="field">
<input type="email" name="email_confirmation" id="email_confirmation" class="form-input input-email_confirmation" placeholder="Re-enter Email" />
</div>
<div class="field">
<input type="password" name="password" id="password" class="form-input input-password" placeholder="Password" />
</div>
<div class="field">
<input type="password" name="password_confirmation" id="password_confirmation" class="form-input input-password_confirmation" placeholder="Re-enter Password" />
</div>
</div>
<div class="button__container">
<button type="submit" name="btn_signup" class="btn-signup">Register</button>
<span>Already have an account? <a href="{{ route('auth.user.login') }}" class="btn-login">Login here.</a></span>
</div>
</form>
</div>
您的 DB::Statement
应该改为:
alter table `users` modify `id` int(10) unsigned zerofill not null auto_increment;
不要 删除 auto_increment
标志。您正在将其放入 alter
.
记住 zerofill
是 mysql 8 中已弃用的功能。它可能会在未来版本中删除。