Laravel如何使用Observer实现日志管理模块

 4364

Laravel使用Observer(观察者)实现日志管理模块

写在前面:

这里实现日志管理写了两篇,第一篇是简单的模型增删改监听并记录日志。第二篇主要介绍的是通过导入文件进行批量数据处理无法很好的被监听处理到,这一部分的数据处理逻辑如何被记录下来。详细请看Laravel日志管理记录导入文件后的数据变化。

1、创建observer文件,我这里是要记录仓库库存模块的操作日志,所以执行下面的语句,会在app/Observers下面创建WarehouseInventoryObserver文件。

  1. php artisan make:observer WarehouseInventoryObserver --model=WarehouseInventory

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

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

在App\Providers\AppServiceProvider下面开启observer

  1. public function boot()
  2. {
  3.     WarehouseInventory::observe(WarehouseInventoryObserver::class);
  4. }

2、监听该模块下的增删改操作,这里使用Repository当然也可以直接使用model。created、updated、deleted分别监听WarehouseInventory模型的新增、更新和删除的操作。


  1. <?php
  2. namespace App\Observers;
  3.  
  4. use App\Models\Warehouse;
  5. use App\Models\WarehouseInventory;
  6. use App\Repositories\ActionLogRepository;
  7. use Illuminate\Support\Arr;
  8. use Illuminate\Support\Facades\Auth;
  9.  
  10. class WarehouseInventoryObserver
  11. {
  12.     protected $user_id;
  13.  
  14.     protected $warehouse;
  15.  
  16.     protected $actionLogRepository;
  17.  
  18.     public function __construct(
  19.         Warehouse $warehouse,
  20.         ActionLogRepository $actionLogRepository
  21.     )
  22.     {
  23.         $this->user_id = Auth::user() ? Auth::user()->id : null;
  24.         $this->warehouse = $warehouse->pluck('name', 'id');
  25.         $this->actionLogRepository = $actionLogRepository;
  26.     }
  27.  
  28.     //创建
  29.     public function created(WarehouseInventory $warehouseInventory)
  30.     {
  31.         if (!empty($this->user_id)) {
  32.             $attributes = $warehouseInventory->getAttributes();
  33.             $attributes = Arr::only($attributes, ['warehouse_id', 'seller_sku', 'quantity', 'box']);
  34.             $warehouse = $this->warehouse->get($attributes['warehouse_id']);
  35.             //拼接数据
  36.             $data = [
  37.                 'module' => 'warehouse_inventory',
  38.                 'user_id' => $this->user_id,
  39.                 'type' => 'create',
  40.                 'content' => [
  41.                     'warehouse' => $warehouse,
  42.                     'seller_sku' => $attributes['seller_sku'],
  43.                     'original_quantity' => 0,
  44.                     'current_quantity' => $attributes['quantity'],
  45.                     'box' => $attributes['box']
  46.                 ]
  47.             ];
  48.  
  49.             $this->actionLogRepository->makeModel()->create($data);
  50.         }
  51.     }
  52.  
  53.     //更新
  54.     public function updated(WarehouseInventory $warehouseInventory)
  55.     {
  56.         if (!empty($this->user_id)) {
  57.             $original = $warehouseInventory->getOriginal();
  58.             $dirty = $warehouseInventory->getDirty();
  59.             $dirty = Arr::except($dirty, ['remark', 'updated_at']);
  60.             if (count($dirty)) {
  61.                 if (Arr::has($dirty, 'warehouse_id')) {
  62.                     $warehouse = $this->warehouse->get($dirty['warehouse_id']);
  63.                 } else {
  64.                     $warehouse = $this->warehouse->get($original['warehouse_id']);
  65.                 }
  66.                 //拼接数据
  67.                 $data = [
  68.                     'module' => 'warehouse_inventory',
  69.                     'user_id' => $this->user_id,
  70.                     'type' => 'update',
  71.                     'content' => [
  72.                         'warehouse' => $warehouse,
  73.                         'seller_sku' => $original['seller_sku'],
  74.                         'original_quantity' => $original['quantity'],
  75.                         'current_quantity' => $dirty['quantity'],
  76.                         'box' => (Arr::has($dirty, 'box')) ? $dirty['box'] : $original['box']
  77.                     ]
  78.                 ];
  79.  
  80.                 $this->actionLogRepository->makeModel()->create($data);
  81.             }
  82.         }
  83.     }
  84.  
  85.     //删除
  86.     public function deleted(WarehouseInventory $warehouseInventory)
  87.     {
  88.         if (!empty($this->user_id)) {
  89.  
  90.             $original = $warehouseInventory->getOriginal();
  91.             $warehouse = $this->warehouse->get($original['warehouse_id']);
  92.             //拼接数据
  93.             $data = [
  94.                 'module' => 'warehouse_inventory',
  95.                 'user_id' => $this->user_id,
  96.                 'type' => 'delete',
  97.                 'content' => [
  98.                     'warehouse' => $warehouse,
  99.                     'seller_sku' => $original['seller_sku'],
  100.                     'original_quantity' => $original['quantity'],
  101.                     'current_quantity' => 0,
  102.                     'box' => $original['box']
  103.                 ]
  104.             ];
  105.  
  106.             $this->actionLogRepository->makeModel()->create($data);
  107.         }
  108.     }
  109. }

3、数据库


60eb21eb75c9b.png


本文网址:https://www.zztuku.com/detail-8999.html
站长图库 - Laravel如何使用Observer实现日志管理模块
申明:如有侵犯,请 联系我们 删除。

评论(0)条

您还没有登录,请 登录 后发表评论!

提示:请勿发布广告垃圾评论,否则封号处理!!

    编辑推荐