-
app/Observers/OrderItemObserver.php
Open in GitHubuse App\Models\OrderLog; use App\Models\OrderItem; class OrderItemObserver { // public function updated(OrderItem $orderItem) { if ($orderItem->isDirty('quantity')) { OrderLog::create([ 'orderId' => $orderItem->orderId, 'orderItemId' => $orderItem->id, 'action' => "updated", 'attribute' => "quantity", 'old_value' => $orderItem->getOriginal('quantity'), 'new_value' => $orderItem->quantity, 'description' => "{$orderItem->product->name} quantity changed from " . $orderItem->getOriginal('quantity') . " to " . $orderItem->quantity, ]); } if ($orderItem->isDirty('amount')) { OrderLog::create([ 'orderId' => $orderItem->orderId, 'orderItemId' => $orderItem->id, 'action' => "updated", 'attribute' => "amount", 'old_value' => $orderItem->getOriginal('amount'), 'new_value' => $orderItem->amount, 'description' => "{$orderItem->product->name} amount changed from " . $orderItem->getOriginal('amount') . " to " . $orderItem->quantity, ]); } } // }
-
app/Providers/ObserverServiceProvider.php
Open in GitHubuse App\Models\Order; use App\Models\OrderItem; use App\Observers\OrderItemObserver; use App\Observers\OrderObserver; use Illuminate\Support\ServiceProvider; class ObserverServiceProvider extends ServiceProvider { // public function boot() { Order::observe(OrderObserver::class); OrderItem::observe(OrderItemObserver::class); } }