Skip to main content

Black Friday 2025! Only until December 1st: coupon FRIDAY25 for 40% off Yearly/Lifetime membership!

Read more here

MdMostaFizurRahaman/ecommerce

29 stars
2 code files
View MdMostaFizurRahaman/ecommerce on GitHub

app/Observers/OrderItemObserver.php

Open in GitHub
use 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 GitHub
use 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);
}
}

We'd Love Your Feedback

Tell us what you like or what we can improve

Feel free to share anything you like or dislike about this page or the platform in general.