Laravel如何使用Observer實現日志管理模塊

laravel使用Observer(觀察者)實現日志管理模塊

寫在前面:
這里實現日志管理寫了兩篇,第一篇是簡單的模型增刪改監聽并記錄日志。第二篇主要介紹的是通過導入文件進行批量數據處理無法很好的被監聽處理到,這一部分的數據處理邏輯如何被記錄下來。詳細請看laravel日志管理記錄導入文件后的數據變化。

1、創建observer文件,我這里是要記錄倉庫庫存模塊的操作日志,所以執行下面的語句,會在app/Observers下面創建WarehouseInventoryObserver文件。

php?artisan?make:observer?WarehouseInventoryObserver?--model=WarehouseInventory

由于模型都是放在app/Models下面,所以要指定路徑。

php?artisan?make:observer?WarehouseInventoryObserver?--model=Models/WarehouseInventory

在AppProvidersAppServiceProvider下面開啟observer

public?function?boot() ????{ ????????WarehouseInventory::observe(WarehouseInventoryObserver::class); ????}

2、監聽該模塊下的增刪改操作,這里使用Repository當然也可以直接使用model。created、updated、deleted分別監聽WarehouseInventory模型的新增、更新和刪除的操作。

<?phpnamespace  AppObservers;use AppModelsWarehouse;use AppModelsWarehouseInventory;use AppRepositoriesActionLogRepository;use IlluminateSupportArr;use IlluminateSupportFacadesAuth;class WarehouseInventoryObserver{     protected $user_id;      protected $warehouse;      protected $actionLogRepository;      public function __construct(         Warehouse $warehouse,         ActionLogRepository $actionLogRepository     )     {         $this->user_id?=?Auth::user()???Auth::user()-&gt;id?:?null; ????????$this-&gt;warehouse?=?$warehouse-&gt;pluck('name',?'id'); ????????$this-&gt;actionLogRepository?=?$actionLogRepository; ????}  ????//創建 ????public?function?created(WarehouseInventory?$warehouseInventory) ????{ ????????if?(!empty($this-&gt;user_id))?{ ????????????$attributes?=?$warehouseInventory-&gt;getAttributes(); ????????????$attributes?=?Arr::only($attributes,?['warehouse_id',?'seller_sku',?'quantity',?'box']); ????????????$warehouse?=?$this-&gt;warehouse-&gt;get($attributes['warehouse_id']); ????????????//拼接數據 ????????????$data?=?[ ????????????????'module'?=&gt;?'warehouse_inventory', ????????????????'user_id'?=&gt;?$this-&gt;user_id, ????????????????'type'?=&gt;?'create', ????????????????'content'?=&gt;?[ ????????????????????'warehouse'?=&gt;?$warehouse, ????????????????????'seller_sku'?=&gt;?$attributes['seller_sku'], ????????????????????'original_quantity'?=&gt;?0, ????????????????????'current_quantity'?=&gt;?$attributes['quantity'], ????????????????????'box'?=&gt;?$attributes['box'] ????????????????] ????????????];  ????????????$this-&gt;actionLogRepository-&gt;makeModel()-&gt;create($data); ????????} ????}  ????//更新 ????public?function?updated(WarehouseInventory?$warehouseInventory) ????{ ????????if?(!empty($this-&gt;user_id))?{ ????????????$original?=?$warehouseInventory-&gt;getOriginal(); ????????????$dirty?=?$warehouseInventory-&gt;getDirty(); ????????????$dirty?=?Arr::except($dirty,?['remark',?'updated_at']); ????????????if?(count($dirty))?{ ????????????????if?(Arr::has($dirty,?'warehouse_id'))?{ ????????????????????$warehouse?=?$this-&gt;warehouse-&gt;get($dirty['warehouse_id']); ????????????????}?else?{ ????????????????????$warehouse?=?$this-&gt;warehouse-&gt;get($original['warehouse_id']); ????????????????} ????????????????//拼接數據 ????????????????$data?=?[ ????????????????????'module'?=&gt;?'warehouse_inventory', ????????????????????'user_id'?=&gt;?$this-&gt;user_id, ????????????????????'type'?=&gt;?'update', ????????????????????'content'?=&gt;?[ ????????????????????????'warehouse'?=&gt;?$warehouse, ????????????????????????'seller_sku'?=&gt;?$original['seller_sku'], ????????????????????????'original_quantity'?=&gt;?$original['quantity'], ????????????????????????'current_quantity'?=&gt;?$dirty['quantity'], ????????????????????????'box'?=&gt;?(Arr::has($dirty,?'box'))???$dirty['box']?:?$original['box'] ????????????????????] ????????????????];  ????????????????$this-&gt;actionLogRepository-&gt;makeModel()-&gt;create($data); ????????????} ????????} ????}  ????//刪除 ????public?function?deleted(WarehouseInventory?$warehouseInventory) ????{ ????????if?(!empty($this-&gt;user_id))?{  ????????????$original?=?$warehouseInventory-&gt;getOriginal(); ????????????$warehouse?=?$this-&gt;warehouse-&gt;get($original['warehouse_id']); ????????????//拼接數據 ????????????$data?=?[ ????????????????'module'?=&gt;?'warehouse_inventory', ????????????????'user_id'?=&gt;?$this-&gt;user_id, ????????????????'type'?=&gt;?'delete', ????????????????'content'?=&gt;?[ ????????????????????'warehouse'?=&gt;?$warehouse, ????????????????????'seller_sku'?=&gt;?$original['seller_sku'], ????????????????????'original_quantity'?=&gt;?$original['quantity'], ????????????????????'current_quantity'?=&gt;?0, ????????????????????'box'?=&gt;?$original['box'] ????????????????] ????????????];  ????????????$this-&gt;actionLogRepository-&gt;makeModel()-&gt;create($data); ????????} ????}}

3、數據庫
Laravel如何使用Observer實現日志管理模塊

相關推薦:最新的五個Laravel視頻教程

? 版權聲明
THE END
喜歡就支持一下吧
點贊8 分享