ardi gunawan
ardi gunawan

Reputation: 425

generate unique id based on timestamp laravel 5

I want to create uniqueid based on timestamps in laravel. In laravel, Timestamps become 2 column, namely created_at and updated_at. like this timestamps laravel

I want to combine created_at and updated_at column for unique id without dash (-) and (:). sample output (for first row in screenshot): 2016091510453920160915104539

I used this code for $transid column:

public function insertFund($request,$lender, $type)
{
    $transid = timestamp();  
    $fund = $this->fund->create([
        'transid' => $transid
    ]);
    return $fund;
}

But, i get error when submit. is it possible to write code for this here?

Thanks in Advance.

Upvotes: 1

Views: 4654

Answers (2)

M Usman Nadeem
M Usman Nadeem

Reputation: 425

i try this, and it works for me

$now = Carbon::now();
$unique_code = $now->format('YmdHisu')

the output is

20180410171800873514

if you parse you can see 2018 04 10 17:18:00 873514

Upvotes: 2

H.Kontodios
H.Kontodios

Reputation: 346

I don't know what exactly you are trying to do. I understood that you want to create a record and fill the transid with concatenated timestamps. The timestamps before the are empty as the record hasn't created yet. In your case i would create the record and after that i would get the timestamps and update the same record.
Example code:

$fund = Fund::create($input);
$transid = $fund->created_at.$fund->updated_at;
$fund->transid = $transid;
$fund->update();

If this is not what you are trying to do , provide more information in order to help.

Upvotes: 0

Related Questions