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

5 篇文章 0 订阅
3 篇文章 0 订阅

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

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

php artisan make:observer WarehouseInventoryObserver --model=WarehouseInventory

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

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

在App\Providers\AppServiceProvider下面开启observer

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

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

<?php

namespace 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、数据库
在这里插入图片描述

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值