Skip to content

Instantly share code, notes, and snippets.

@slow-is-fast
Created May 18, 2020 08:18
Show Gist options
  • Save slow-is-fast/c62758ef1d552bb9ef179c2689abd92e to your computer and use it in GitHub Desktop.
Save slow-is-fast/c62758ef1d552bb9ef179c2689abd92e to your computer and use it in GitHub Desktop.
Laravel Model observer get changed data
    public function updated(Order $order)
    {
        $originals = [];
        if ($order->isDirty('order_status')) {
            Log::info("order new status :" . $order->order_status . " -- old status :" . $order->getOriginal('order_status'));
        }
        foreach ($order->getDirty() as $key => $val) {
            $originals[$key] = $order->getOriginal($key);
        }

        Log::info(json_encode([
            'changed' => $order->getDirty(),
            'originals'=>$originals,
        ]));

    }
@slow-is-fast
Copy link
Author

code in OrderObserve

created by php artisan make:observe OrderObserve --model=Order

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment