Home > Blockchain >  ID column default value error despite autoincrement
ID column default value error despite autoincrement

Time:05-30

I am new to laravel and I am trying to create a user database. But I am getting an error message regarding my ID column even though I set it to autoincrement. enter image description here

Here is my migration

<?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');
    }
};

I haven't done anything yet in the model so it's still at default

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Users extends Model
{
    use HasFactory;
}

Here is my POST method

<?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!');
        }
    }
}

Here is my signup form

<div >
    <form action="{{ route('auth.user.reguser') }}" method="POST">
        @if (Session::has('success'))
            <div >{{ Session::get('success') }}</div>
        @endif
        @if (Session::has('fail'))
            <div >{{ Session::get('fail') }}</div>
        @endif
        @csrf
        <h1 >SIGN UP</h1>
        <div >
            <div >
                <input type="text" name="firstname" id="firstname"  placeholder="First Name" value="{{ old('firstname') }}" /><input type="text" name="lastname" id="lastname"  placeholder="Last Name" value="{{ old('lastname') }}" />
            </div>
            <div >
                <input type="email" name="email" id="email"  placeholder="Email" value="{{ old('email') }}"" />
            </div>
            <div >
                <input type="email" name="email_confirmation" id="email_confirmation"  placeholder="Re-enter Email" />
            </div>
            <div >
                <input type="password" name="password" id="password"  placeholder="Password" />
            </div>
            <div >
                <input type="password" name="password_confirmation" id="password_confirmation"  placeholder="Re-enter Password" />
            </div>
        </div>

        <div >
            <button type="submit" name="btn_signup" >Register</button>
            <span>Already have an account? <a href="{{ route('auth.user.login') }}" >Login here.</a></span>
        </div>
    </form>
</div>

CodePudding user response:

Your DB::Statement should have the following instead:

alter table `users` modify `id` int(10) unsigned zerofill not null auto_increment;

DO NOT drop the auto_increment flag. You are dropping it in your alter.

REMEMBER zerofill is a deprecated feature in 8. It may be dropped on future version.

  • Related