Home > database >  How to get unique transaction number using laravel?
How to get unique transaction number using laravel?

Time:05-10

I am using Laravel ID Generator and I have made transaction ID and inserted into the database but unfortunately I am getting duplicate transaction ID error.

How can I receive a unique ID?

Controller

public function nonMemberBookingUserSummaryStore(Request $request,$random_code){

    $room = Room::where('random_code',$random_code)->first();
    $booking  = Booking::with('bookingDetails')->where('room_id',$room->id)
      ->orderBy('id','DESC')->first();


    $transaction_id = IdGenerator::generate(['table' =>
    'transactions',
    'field'=>
    'transaction_id',
    'length' => 15,
    'prefix' =>"NMB-BOO-"]);

    $transaction_info = $request->session()->get('name') .' booked '. $room->name .' 
    RM '. $booking->price;

    $shopTransaction                                   = new Transaction();
    $shopTransaction->booking_id                       = $booking->id;
    $shopTransaction->non_member_full_name             = $request->session()->get('name');
    $shopTransaction->non_member_email                 = $request->session()->get('email');
    $shopTransaction->non_member_mobile_number         = $request->session()->get('mobile_number');
    $shopTransaction->non_member_company_name          = $request->session()->get('company_name');
    $shopTransaction->transaction_type                 = 3;
    $shopTransaction->amount_credit                    = $booking->price;
    $shopTransaction->transaction_info                 = $transaction_info;
    $shopTransaction->transaction_date_time            = date('Y-m-d h:i');
    $shopTransaction->transaction_id   =  $transaction_id.'-'. date('Y H:i:s');

    $shopTransaction->save();

    return redirect()->route('non-member-booking-success',$random_code);

}

CodePudding user response:

You can try this function:

@php
 function unique_id($digits)
 {
return substr(base_convert(sha1(uniqid(mt_rand())), 16, 36), 0, $digits);
 }
 $transaction_id = unique_id(12); // 12 digits
@endphp

CodePudding user response:

You can implement your own id generator like this.... Change the model that you need to generate ID

Add new folder in your app directory at name of Helpers...

And then create new file InvoiceNumberHelper.php

Copy paste the code.... Below

I think it's helps much

<?php

namespace App\Helpers;

use App\Models\UserTransaction\UserTransaction;
use DateTime;

class InvoiceNumberHelper
{


    public static function GenerateInvoiceNumber():string
    {

       try{

        $current_date_time = new DateTime();

        $date_sequence = $current_date_time->format("dmy");

        //section generate the sequence of running number of trip sheet

        $lastTransactionId = UserTransaction::orderBy('tr_id', 'desc')->first();

        if (!$lastTransactionId)
            // We get here if there is no TripSheet at all
            // If there is no Trip sheet number set it to 0, which will be 1 at the end.
            $number = 0;
        else

            $number = substr($lastTransactionId->tr_id, 8);


        return "IN".$date_sequence. sprintf('d', intval($number)   1);

       }
       catch (\Exception $e)
       {
           dd($e);
       }

    }


}

You can now call this static method which returns you a unique string on invoice number every time

Use on controller like this

$invoice_no=InvoiceNumberHelper::GenerateInvoiceNumber();

  • Related