Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

AbdulBazith's avatar

How to generate random number with string and todays date

Guys iam working with a project inventory control like system.

i have a purchase order form. the purchase_order_number will be automatically generated.

what i did is,

fetched the last id of the purchase table and added few strings POID and retruned it the the view. so each and every time there will be new purchase order id.

but i faced many problems in that.

whats happening is i will check if any record is there in table. if not there the order id must be POID1 else if any record available then take the id add 1 with the id then add the string.

this is what i will do


 $pur_id=PurchaseOrder:: select('id')->orderBy('created_at','asc')->first();

        if($pur_id==null)
         {
             $pur_id='POID1';
         }

         else
         {
             $pur_id->id;

         
           $pur_id =$pur_id->id+1;

         
          $pur_id= 'POID'.$pur_id;

         }

         dd($pur_id);

but all the time this wont work. because some times the order id becomes same due to Internet reload. like that issues.

if i delete all the records from the table.

the first order number fills right, but after the the order number changes without any serial number because it takes the deleted id of that.

now whats my expectation is, need a unique purchase order id, with POID with series of numbers like POID1, POID2, POID3, POID4 likes this need to go on, else can i add todays date with that id like that.

Kindly suggest, any idea please and help me.

0 likes
10 replies
Snapey's avatar

Does it matter if there is a gap?

Can you not use the Auto incrementing database row ID as the count?

AbdulBazith's avatar

@snapey thank you for you response.

i cant understand. the gap is for just to show the code.

and i cant understand "Auto incrementing database row ID as the count"

can u slightly explain please

Snapey's avatar

Apart from that, there are big issues with your code.


// needs to be descending not ascending to get the latest entry
 $pur_id=PurchaseOrder:: select('id')->orderBy('created_at','asc')->first();

        if($pur_id==null)
         {
             $pur_id='POID1';
         }

         else
         {

//what is this line doing?
             $pur_id->id;

 //transforming pur_id from a Purchase Order record to an integer (did you give up thinking of names)
           $pur_id =$pur_id->id+1;

         
          $pur_id= 'POID'.$pur_id;

         }

         dd($pur_id);

Don't do anything until you are ready to store the Purchase order record

Then just store it.... hey- its got a unique number now.

Anytime you DISPLAY the purchase order, add the text POID to it. You can do this with an accessor on your model.

You probably also ought to pad with leading zeros.

public function getPOIDAttribute()
{
    return 'POID' . str_pad($this->id,5,'0',STR_PAD_LEFT);
}

AbdulBazith's avatar

@snapey thank you for your clear explanation.

Now i erased

all my if and else codings.

just added the below code in my model

public function getPOIDAttribute()
{
    return 'POID' . str_pad($this->id,5,'0',STR_PAD_LEFT);
}

this is my controller create method

 return view('Purchase.add-purchase-order-list')


and this is my add purchase order blade file


<div class="col-md-6"><label class="control-label">Purchase Order
                                            ID <span class="asterisk">*</span></label>
                                        <div class="">
                                            <input type="text" name="pur_order_num" id="pur_order_num"
                                                class="form-control" value="{{ $pur_id }}" required readonly />
                                        </div>
                                    </div>

Actually i have a separate column for purchase order number. these are my columns


id
login_user_id
pur_order_num
supplier_id
order_date
order_time
expec_date
expec_time
created_at
updated_at


now what should id do it to display in my blade file.?

Snapey's avatar

Since you have created a separate column for the PO number, then you have no choice but a two-step process, one to find the next number and one to write it to the column.

Do you really need that? Can you not just use the row id in the table?

The gotcha to this is that there can be gaps in the IDs if say a PO is in draft state and is then deleted. This leaves a gap which is not good from an auditing point of view.

If you use a separate number column, then you can find the highest and then add one to create the next number. However, beware again. If your site is heavily used, could two people create a purchase order at the same time and get the same number?

If it were just for my own use (as below) this is what I do;

            $salesOrder = SalesOrder::create([
                                'customer_id' => $customer->id,
                                'status' => 'New',
                                'type' => $type,
                                'salesorder' => SalesOrder::max('salesorder') +1,
                                ]);
        

salesorder column is the same as your pur_order_num

I set its value to be the result of a query to read the current max number + 1

Then, I decorate this plain number in the views as per previous examples

Snapey's avatar

@jlrdw bit unfair pulling him up for me not explaining myself properly.

jlrdw's avatar

Sorry for any misunderstanding.

jlrdw's avatar
$a = date('m-d-Y').substr(fmod(microtime(true), 1), 1);
$b = str_replace("-","",$a);
$c = 'PO-' . str_replace(".","-",$b);
PO-03312019-52710294723511

Please or to participate in this conversation.