I have a relationship in MainOrder model class like this:
public function transaction()
{
return $this->belongsTo(Transaction::class, 'transaction_id', 'id');
}
And in my MainOrder's pay() function, I have a code block like this:
$mainPayment = $this->transaction()->create([
'receiver_id' => Wallet::MAIN_WALLET,
'sender_id' => $this->consumer->cash_wallet->id,
'amount' => $this->amount,
'transaction_type_id' => TransactionType::getMainOrderPaymentId(),
'transaction_code' => 'main_order_' . Str::uuid()
]);
According to Laravel's documentation at
- https://laravel.com/docs/7.x/eloquent-relationships#inserting-and-updating-related-models
- https://laravel.com/docs/7.x/eloquent-relationships#the-create-method
I am acutally expecting the mainOrder instance's transaction_id to be automatically updated once the $mainPayment is created successfully. However, the record is saved into DB with transaction_id = null. And the pay() function is within a DB::transaction
Which part did I do wrong here? Thank you for you help!