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

rubendn's avatar

Observer not saving insert on another model

I have an observer on one model's 'Updated' event. It is supposed to create a new database entry in another model if there is a change to one of the fields. I know the observer is being triggered because I've used a DD inside the if statement below. It just seems that the save() does not persist the data to the database. Any ideas? I'm probably doing something wrong inside an observer. The DD statement below does show what should be the correct values for each.

    public function updated(Item $item): void
    {
        if($item->ItemQuantity <> $item->getOriginal('ItemQuantity')){
            $InventoryChange = new InventoryChange;
            $InventoryChange->ItemID = $item->ItemID;
            $InventoryChange->InventoryChangeNote = session('InventoryChangeNote');
            $InventoryChange->Quantity = $item->ItemQuantity - $item->getOriginal('ItemQuantity');
            $InventoryChange->save();
            dd('Item Quantity New: ' . $item->ItemQuantity . ' : ' . 'Item Quantity Old: ' . $item->getOriginal('ItemQuantity') . ' : ' . 'Session: ' . session('InventoryChangeNote') . ' : ItemID: ' . $item->ItemID . " : Change Amount: " . $item->ItemQuantity - $item->getOriginal('ItemQuantity'));
            session(['InventoryChangeNote' => '']);
        }
    }
0 likes
10 replies
LaryAI's avatar
Level 58

The issue might be with the session. Try saving the session before saving the InventoryChange model. Also, make sure that the InventoryChange model is imported at the top of the observer file. Here's the updated code:

use App\Models\InventoryChange;

public function updated(Item $item): void
{
    if($item->ItemQuantity <> $item->getOriginal('ItemQuantity')){
        session(['InventoryChangeNote' => '']);
        $InventoryChange = new InventoryChange;
        $InventoryChange->ItemID = $item->ItemID;
        $InventoryChange->InventoryChangeNote = session('InventoryChangeNote');
        $InventoryChange->Quantity = $item->ItemQuantity - $item->getOriginal('ItemQuantity');
        $InventoryChange->save();
        dd('Item Quantity New: ' . $item->ItemQuantity . ' : ' . 'Item Quantity Old: ' . $item->getOriginal('ItemQuantity') . ' : ' . 'Session: ' . session('InventoryChangeNote') . ' : ItemID: ' . $item->ItemID . " : Change Amount: " . $item->ItemQuantity - $item->getOriginal('ItemQuantity'));
    }
}
rubendn's avatar

InventoryChange model is imported and Session values are as they should be.

Jsanwo64's avatar

@rubendn Based on the code you have provided, it looks like you are on the right track. The save() method should persist the data in the database. However, there could be a few things that are causing the data not to be saved.

One possible issue could be that there is a validation rule on the InventoryChange model that is preventing the data from being saved. You can try temporarily disabling any validation rules to see if this is the issue by using the saveQuietly() method instead of save().

Another possible issue could be related to the database transaction. When you save data to the database, Laravel wraps the operation in a transaction. If an error occurs during the transaction, the data will not be saved. You can try disabling the transaction by using the saveOrFail() method instead of save().

Here's an updated version of your code that uses saveQuietly() and saveOrFail() to see if either of these solutions works:

public function updated(Item $item): void
{
    if($item->ItemQuantity <> $item->getOriginal('ItemQuantity')){
        $InventoryChange = new InventoryChange;
        $InventoryChange->ItemID = $item->ItemID;
        $InventoryChange->InventoryChangeNote = session('InventoryChangeNote');
        $InventoryChange->Quantity = $item->ItemQuantity - $item->getOriginal('ItemQuantity');
        $InventoryChange->saveQuietly(); // try disabling validation rules
        // $InventoryChange->saveOrFail(); // try disabling transaction
        dd('Item Quantity New: ' . $item->ItemQuantity . ' : ' . 'Item Quantity Old: ' . $item->getOriginal('ItemQuantity') . ' : ' . 'Session: ' . session('InventoryChangeNote') . ' : ItemID: ' . $item->ItemID . " : Change Amount: " . $item->ItemQuantity - $item->getOriginal('ItemQuantity'));
        session(['InventoryChangeNote' => '']);
    }
}

If neither of these solutions works, it may be helpful to check the database logs or error logs to see if there are any errors or exceptions being thrown when you try to save the data.

rubendn's avatar

I changed the save line to :

$saved = $InventoryChange->save();

And $saved is returning TRUE but the data is not saved to the database.

Snapey's avatar

how have you implemented this observer?

Any errors in the Log files?

Personally, I would fire an event with the details of the updated model and then have a listener create the related model.

rubendn's avatar

I think I've figured out the cause but not a way to fix it.

The UPDATE on the ITEM model that is triggering the observer is inside a DB TRANSACTION. If I take it out of the TRANSACTION, it saves correctly. I would still like the update on the ITEM model inside a transaction.

Any ideas?

rubendn's avatar

Self-caused issue. Since the DD I was using for debugging was inside the observer that was triggered inside the TRANSACTION, the changes were not being committed. Once I removed the DD, the changes are committing properly.

newbie360's avatar

@rubendn

the table columns all named like this ?

ItemID
InventoryChangeNote
Quantity

or

item_id
inventory_change_note
quantity

check both model $fillable is correct

if you wrap $item->save() in Transaction in controller, your code should work

    public function updated(Item $item): void
    {
        if($item->ItemQuantity <> $item->getOriginal('ItemQuantity')){
            $InventoryChange = new InventoryChange;
            $InventoryChange->ItemID = $item->ItemID;
            $InventoryChange->InventoryChangeNote = session('InventoryChangeNote');
            $InventoryChange->Quantity = $item->ItemQuantity - $item->getOriginal('ItemQuantity');
            $InventoryChange->save();

            session(['InventoryChangeNote' => '']);
        }
    }

after $InventoryChange->save();, getOriginal() is useless.

rubendn's avatar

@newbie360 It is working now. Issue was the DD I was using for troubleshooting was not allowing the DB transaction of the Item Update to commit.

Please or to participate in this conversation.