Written in front:
I have written two articles here to implement log management. The first article is a simple model addition, deletion and modification monitoring and log recording. The second article mainly introduces how batch data processing through imported files cannot be well monitored and processed. How to record this part of the data processing logic. For details, please see Laravel Log Management to record data changes after importing files.
1. Create the observer file. I want to record the operation log of the warehouse inventory module, so executing the following statement will create the WarehouseInventoryObserver file under app/Observers.
php artisan make:observer WarehouseInventoryObserver --model=WarehouseInventory
Since the models are placed under app/Models, the path must be specified.
php artisan make:observer WarehouseInventoryObserver --model=Models/WarehouseInventory
Enable observer under App\Providers\AppServiceProvider
public function boot() { WarehouseInventory::observe(WarehouseInventoryObserver::class); }
2. Monitor the addition, deletion and modification operations under this module. Of course, you can use Repository here or you can use model directly. created, updated, and deleted respectively monitor the new, updated, and deleted operations of the WarehouseInventory model.
<?phpnamespace App\Observers;use App\Models\Warehouse;use App\Models\WarehouseInventory;use App\Repositories\ActionLogRepository;use Illuminate\Support\Arr;use Illuminate\Support\Facades\Auth;class WarehouseInventoryObserver{ protected $user_id; protected $warehouse; protected $actionLogRepository; public function __construct( Warehouse $warehouse, ActionLogRepository $actionLogRepository ) { $this->user_id = Auth::user() ? Auth::user()->id : null; $this->warehouse = $warehouse->pluck('name', 'id'); $this->actionLogRepository = $actionLogRepository; } //创建 public function created(WarehouseInventory $warehouseInventory) { if (!empty($this->user_id)) { $attributes = $warehouseInventory->getAttributes(); $attributes = Arr::only($attributes, ['warehouse_id', 'seller_sku', 'quantity', 'box']); $warehouse = $this->warehouse->get($attributes['warehouse_id']); //拼接数据 $data = [ 'module' => 'warehouse_inventory', 'user_id' => $this->user_id, 'type' => 'create', 'content' => [ 'warehouse' => $warehouse, 'seller_sku' => $attributes['seller_sku'], 'original_quantity' => 0, 'current_quantity' => $attributes['quantity'], 'box' => $attributes['box'] ] ]; $this->actionLogRepository->makeModel()->create($data); } } //更新 public function updated(WarehouseInventory $warehouseInventory) { if (!empty($this->user_id)) { $original = $warehouseInventory->getOriginal(); $dirty = $warehouseInventory->getDirty(); $dirty = Arr::except($dirty, ['remark', 'updated_at']); if (count($dirty)) { if (Arr::has($dirty, 'warehouse_id')) { $warehouse = $this->warehouse->get($dirty['warehouse_id']); } else { $warehouse = $this->warehouse->get($original['warehouse_id']); } //拼接数据 $data = [ 'module' => 'warehouse_inventory', 'user_id' => $this->user_id, 'type' => 'update', 'content' => [ 'warehouse' => $warehouse, 'seller_sku' => $original['seller_sku'], 'original_quantity' => $original['quantity'], 'current_quantity' => $dirty['quantity'], 'box' => (Arr::has($dirty, 'box')) ? $dirty['box'] : $original['box'] ] ]; $this->actionLogRepository->makeModel()->create($data); } } } //删除 public function deleted(WarehouseInventory $warehouseInventory) { if (!empty($this->user_id)) { $original = $warehouseInventory->getOriginal(); $warehouse = $this->warehouse->get($original['warehouse_id']); //拼接数据 $data = [ 'module' => 'warehouse_inventory', 'user_id' => $this->user_id, 'type' => 'delete', 'content' => [ 'warehouse' => $warehouse, 'seller_sku' => $original['seller_sku'], 'original_quantity' => $original['quantity'], 'current_quantity' => 0, 'box' => $original['box'] ] ]; $this->actionLogRepository->makeModel()->create($data); } }}
3. Database
The above is the detailed content of How Laravel uses Observer to implement log management module. For more information, please follow other related articles on the PHP Chinese website!