1 year ago

How to create auto generated ID suffix? e.g A-001, A-002, so on and so forth

Posted 1 year ago by aulapantheon

Hello guys, I want to create the automatic generated custom ID.

Here's the thing, I have fields in database in which 'id' and 'product_code'. I want to auto- generate the product code based on last inserted ID. So, if it's get inputted, it becomes 'A-001, A-002', so on. It's simply suffixing the 'product_code' field with '1,2,..' with 'A-00' as the preliminary. How do I do that? Been doing this yesterday night but has no light on it.

This is the paste I've been working on:

public function store(Request $request) {

    $init = new Postest;
    $input = $request->all();
    $init->kd_barang = 'A-00' . $init->id;
    $postest = Postest::create($input, $init);
    return response()->json([
        'success' => true,
        'message' => 'Post Created'

Error Laravel Message:

SQLSTATE[HY000]: General error: 1364 Field 'kd_barang' doesn't have a default value (SQL: insert into postests (nama_roti, harga, updated_at, created_at) values (Roti Dika, 8000, 2018-05-08 04:11:45, 2018-05-08 04:11:45))

Note: kd_barang is product_code in Indonesian

Please sign in or create an account to participate in this conversation.