zoukankan      html  css  js  c++  java
  • Laravel使用Observer(观察者)

     

    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模型的新增、更新和删除的操作。

    <?php
    
    namespace 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()->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、数据库

  • 相关阅读:
    软件开发者路线图 从学徒到高手 读后感
    Greplin:搜索你的所有在线社交内容
    黄小琥 / 简单/不简单 / 没那么简单
    讯雷 云系列 不错
    来自Amazon、Cloudera、Microsoft与IBM的HadoopasaService
    FAQs and feedback
    I've Never Been To Me by Charlene with lyrics
    烧香网 历害
    4A公司
    一个新的比较网站,做的不错
  • 原文地址:https://www.cnblogs.com/myJuly/p/14079395.html
Copyright © 2011-2022 走看看