'How to assign user ID automatically in PHP upon user register?

I am looking at creating user ID automatically upon user registration. I've coded the files using Laravel and the database uses MySQL. The format should be S0001, S0002, S0003, so on and so forth.

What kind of datatype I should change it to in MySQL? Currently it is set at BIGINT.

If yes, how do I proceed with the code then?

This code below is from viewuser.blade.php:

{{View:: make('title')}}
{{View:: make('menu')}}

<div class="table-responsive">
    <table class="table table-striped table-sm">
        <!-- <caption>List of Users</caption> -->
            <thead>
                <tr>
                    <th scope="col">#</th>
                    <th scope="col">User ID</th>
                    <th scope="col">Full Name</th>
                    <th scope="col">Email Address</th>
                    <th scope="col">Password</th>
                    <th scope="col">Action</th>
                </tr>
            </thead>

            <tbody>
            @foreach($users as $user)
                <tr>
                    <td>{{ $loop->iteration}}</td>
                    <td>{{ $loop->iteration}}</td>
                    <td>{{ $user->name }}</td>
                    <td>{{ $user->email }}</td>
                    <td>{{ $user->password }}</td>
                    <td>{{ date('D, d F Y', strtotime($user->created_at)) }}</td>
                    <td>
                        <a href="/editmyuser?rid={{ $user->id }}">Edit</a>
                        <a href="/editmyuser?rid={{ $user->id }}">Delete</a>
                    </td>
                </tr>
                @endforeach
            </tbody>
    </table>
</div>

<div class="pagination">
    {{ $users->links() }}
</div>

And this is the Register Page code, in case it's useful:

{{View:: make('title')}}

<div>
    <form action="register" method="post">
    @csrf

        <div>
            <label for="exampleInputEmail1">Full Name</label>
            <input type="text" name="fullname" id="exampleInputName1">
        </div>

        <div>
            <label for="exampleInputEmail1">Email Address</label>
            <input type="email" name="email" id="exampleInputEmail1" aria-describedby="emailHelp">

            <div id="emailHelp">We'll never share your email with anyone else
            </div>
        </div>

        <div>
            <label for="exampleInputPassword1">Password</label>
            <input type="password" name="password" id="exampleInputPassword1">
        </div>

        <button type="submit">Register</button>
        <button type="button" onclick="javascript:history.back()">Cancel</button>

    </form>
</div>

{{View:: make('footer')}}


Solution 1:[1]

Why not just use the existing id column on the Users table, then define a prefix column in your Users table that you can prepend to the id whenever you want to display it.

Users table migration

$table->string('prefix')->default('S');

Users model

Then you can do something like define an accessor on your model to return the combined value of the id and prefix columns:

class User extends Model
{
    protected function matriculationNumber()
    {
        return $this->prefix . str_pad($this->id, 3, '0', STR_PAD_LEFT);
    }
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Peppermintology