0

I'm working with Laravel Filament where I'm using audits package for audits the activity of laravel model. I've laravel model like:

<?php

namespace App\Models;

use App\Casts\Money;
use App\Observers\InventoryObserver;
use Illuminate\Database\Eloquent\Attributes\ObservedBy;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasOne;
use Illuminate\Database\Eloquent\SoftDeletes;
use OwenIt\Auditing\Contracts\Auditable;

#[ObservedBy(InventoryObserver::class)]
class Inventory extends Model implements Auditable
{
    use \OwenIt\Auditing\Auditable;
    use HasFactory, SoftDeletes;

    protected $guarded = [];
}

And InventoryObserver.php

<?php

namespace App\Observers;

use App\Events\RefreshInventories;
use App\Events\RefreshProducts;
use App\Models\Inventory;
use App\Services\TaxCalculatorService;

class InventoryObserver
{
   public function created(Inventory $inventory): void
    {
        if ($inventory->gross_amount) {
            $taxCalculatorService = TaxCalculatorService::make($inventory->gross_amount, $inventory->tax_rate);

            $inventory->net_amount = $taxCalculatorService->getNetAmount();
            $inventory->tax_amount = $taxCalculatorService->getTaxAmount();

            $inventory->saveQuietly();
        }

        $inventory->machine->updateStockPercentage();
    }

    public function updated(Inventory $inventory): void
    {
        if ($inventory->isDirty(['gross_amount', 'tax_rate'])) {
            $taxCalculatorService = TaxCalculatorService::make($inventory->gross_amount, $inventory->tax_rate);

            $inventory->net_amount = $taxCalculatorService->getNetAmount();
            $inventory->tax_amount = $taxCalculatorService->getTaxAmount();

            $inventory->saveQuietly();

            //            RefreshProducts::broadcast($inventory->machine);
        }

        if ($inventory->isDirty('quantity', 'max_quantity')) {
            $inventory->machine->updateStockPercentage();

            //            RefreshInventories::broadcast($inventory->machine);
        }
    }

When I'm saving values from form it's auditing the values in audits database table with empty values inside old_values and new_values. enter image description here

Can anyone help me how can I solve this problem?

Thanks...

1 Answer 1

2

I think because of saveQuietly(). Because it suppresses all events ( updating, updated) as I remember.

Try using

$inventory->save(); # doubt check for infinity loop inside `update`

If you must use saveQuietly() but still want auditing, then use this manually

$inventory->setAuditEvent('updated');
$inventory->audit();
Sign up to request clarification or add additional context in comments.

1 Comment

I must need to use saveQuietly(). I've use your code but I got error like: Call to undefined method App\Models\Inventory::audit() . I've already added Audit trait and all on Inventory model. I've done code like: $inventory->saveQuietly();$inventory->setAuditEvent('updated');$inventory->audit();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.