I want to give my database record a unique code
that contains a string of VDR-000X
(X
should be auto incremented).
So far, I have created a logic in my PHP file that look like this. It works fine but since it should be generated in the server side, I think doing this on MySQL so that it fills up the field automatically by default (like the id
field) is more appropriate. But I don't know how. Is there any SQL commands that can do this?
My implementation uses Laravel framework, answers with laravel migration blueprint is prefered but native MySQL query is fine too and I will try convert it myself. Please give it a shot!
/**
* Store a newly created resource in storage.
*
* @param \App\Http\Requests\StoreVendorRequest $request
* @return \Illuminate\Http\Response
*/
public function store(StoreVendorRequest $request)
{
// We want it to fill up the `code` field later, so we made a variable for the validated request.
// User should not be able to input this manually so we do this here
// on the server side. The `code` is permanent and shouldn't be updated either,
// just like the `id` field
$validated = $request->validated();
// Before we fill the code, grab the `id` of the latest record
$lastId = Vendor::latest()->orderBy('id', 'desc')->first()->id;
// Increment the `$lastId` and give it at least 4 leading zeros
$code = Str::padLeft($lastId++, 4, '0');
// Set the `code` value
$validated['code'] = "VDR-{$code}";
// Store it in the database
$created = Vendor::create($validated);
// Return the result
return new VendorResource($created);
}
The result should look like this
+---------------------------------------------------------------------------+
| id | name | code | created_at | updated_at |
+---------------------------------------------------------------------------+
| 1 | something | VDR-0001 | 2022-03-19 00:00:00 | 2022-03-19 00:00:00 |
| 2 | something2 | VDR-0002 | 2022-03-19 00:00:00 | 2022-03-19 00:00:00 |
| ... | ... | ... | ... | ... |
| ... | ... | ... | ... | ... |
| 123 | something123 | VDR-0123 | 2022-03-19 00:00:00 | 2022-03-19 00:00:00 |
+---------------------------------------------------------------------------+
EDIT
I found this similar question about How to make MySQL table primary key auto increment with some prefix but it doesn't do what I want to do which is actually store the code automatically by that format in the field and let the id
as is.