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 分享