combine two column (and also 'unit_price' ??) to one column item_id (FK)
$table->string('item_code');
$table->string('item_name');
if use "item001", how you know the number of 0 zero give to str_pad()
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
Hey there, I have a migration table like this
public function up()
{
Schema::create('purchase_details', function (Blueprint $table) {
$table->id();
$table->unsignedBigInteger('purchase_id');
$table->string('item_code');
$table->string('item_name');
$table->float('unit_price');
$table->float('qty');
$table->flat('discount_on_all')->default(0);
$table->float('net_amount');
$table->timestamps();
});
}
i want to generate item_code for a product while insertion, the rest of the fields are dynamic. do I don't want random number generators, should be like this "item001", or any other prefer way
@maaz how about you use the id of the puchase_details that you created so that you have a unique item_code?
// after you created the purchase_details
$itemCode = 'item' . str_pad($purchase_details->id, 4, '0', STR_PAD_LEFT);
$purchase_details->item_code = $itemCode;
// item_code will be item0001 if id is 1, you get the point
$purchase_details->save();
and if you want to expand to item00000001 just change 4 to 8 or what you prefer.
Please or to participate in this conversation.