Home > database >  How I can generate the unique transaction ID in laravel 8?
How I can generate the unique transaction ID in laravel 8?

Time:05-01

I am using Laravel ID Generator and i have made trasaction id and inserted in db but unfortunately i am getting duplicate transaction id in db please help me how i can unique ? thank u.

please check transaction id column.

enter image description here

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;
            $shopTransaction->save();

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

        }
    }

CodePudding user response:

You can use uuid() function, which is already inside of laravel. Here some example

Transaction.php

use Illuminate\Support\Str;
class blabla extends blah
{
    public $incrementing = false;
    protected $keyType = 'string';
    protected $guarded = ['id'];
    // your relationship and anything 
   ...
   protected static function booted() {
      static::creating(function ($model) {
        $model->id = Str::uuid();
   });
}

Just make sure your migration file for transaction is right type of column

CodePudding user response:

In the Transaction model class use the event created to get de ID and concatenate to transaction_id :

class Transaction extends Model {
 ...
 public static function boot()
 {
        parent::boot();
        self::created(function ($model) { 
            $model->transaction_id = 'NMB-BOO-' . str_pad($model->id, 7, "0", STR_PAD_LEFT);
            $model->save();
        });
 }
}
  • Related