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

dipherent's avatar

How to update a field in another table when creating a model

Hi all,

I have two tables in a database: Account & Transactions. I have all the relations setup right.

When I create a transaction, let's say "Withdarw" money from the account, I want the account balance to be updated with the new value (transaction amount minus balance).

Here is what I have in my livewire controller:

public function transactionData()
    {
        return [
            'item' => $this->item,
            'amount' => $this->amount,
            'type' => $this->type,
            'account_id' => $this->account,
          
        ];
    }

    public function create()
    {
        $this->validate();
        $transaction = Transaction::create($this->transactionData());
        
            
        }

How do I update the account model with the new balance?

Any help is highly appreciated

0 likes
8 replies
jlrdw's avatar

Usually a balance is a derived column. A running total. But can be hard coded. If displaying elsewhere use the aggregate function SUM to show totals.

1 like
dipherent's avatar

Thank you @jlrdw , but is there an easier way to do this with eloquent?

The app I am building is so simple and here is the logic:

I am starting a balance in the account model and I entered that manually. Let’s say $1000

Now I want to create a transaction and when the type is deposit I want to update the $1000 with the deposited amount. When the type of transaction is withdraw I want to subtract that and update the balance field in the account table.

I thought maybe there is one line I can add after the create method when creating the transaction.??

Thanks again

jlrdw's avatar

@dipherent normally you do not have a balance field, as I said the balance is a calculated running total.

For a summary at the bottom of things like reports you use the aggregate SUM.

Usually accounts is a one-to-many relation. Unless you are creating separate tables for each type of transaction, though I couldn't imagine doing it that way.

I would suggest taking a basic bookkeeping lesson.

2 likes
dipherent's avatar

@jlrdw Thank you, indeed taking a bookkeeping tut would help give me a better idea. The app I am building is for learning purposes and I was looking for a simple solution, but I will keep your advice in mind if I decide to take this further.

Many thanks for your valuable tips

MohamedTammam's avatar
Account::where('id', $account_id)->decrement('balance', $amount);
2 likes
frankielee's avatar
Level 29

You have several ways to perform that.

  1. In the same function. I assume you have setup the correct relationship
 public function create()
{
        $this->validate();
        $transaction = Transaction::create($this->transactionData());
        $transaction->account()->decrement('balance', $amount);
}
  1. By using the eloquent events, ref: https://laravel.com/docs/9.x/eloquent#events
1 like
dipherent's avatar

@frankielee Great, this worked perfect. I also added the "increment" helper to increase the balance when a deposit is made. This is what I was looking for ,... MANY THANKS

Please or to participate in this conversation.