<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
alexstewartja / laravel-attribute-observer example snippets
return [
/*
|--------------------------------------------------------------------------
| Attribute Observers
|--------------------------------------------------------------------------
|
| Here you may configure all desired Models and their respective Attribute
| Observers. For example:
|
| 'observers' => [
| \App\Models\Order::class => [
| \App\AttributeObservers\OrderStatusObserver::class,
| ],
| ]
|
*/
'observers' => [
// Define your model & attribute observers here...
]
];
namespace App\AttributeObservers;
use App\Models\Order;
class OrderStatusObserver
{
/**
* Handle changes to the "id" field of Order on "created" events.
*
* @param \App\Models\Order $order
* @param mixed $newValue The current value of the field
* @param mixed $oldValue The previous value of the field
* @return void
*/
public function onIdCreated(Order $order, mixed $newValue, mixed $oldValue)
{
//
}
}
namespace App\AttributeObservers;
use App\Events\OrderStatusChanged;
use App\Models\Order;
class OrderStatusObserver
{
/**
* Handle changes to the "status" field of Order on "saved" events.
*
* @param \App\Models\Order $order
* @param mixed $newValue The current value of the field
* @param mixed $oldValue The previous value of the field
* @return void
*/
public function onStatusSaved(Order $order, mixed $newValue, mixed $oldValue)
{
// Dispatch an event that sends an order update email to the customer
OrderStatusChanged::dispatch($order);
}
}