亚洲视频二区_亚洲欧洲日本天天堂在线观看_日韩一区二区在线观看_中文字幕不卡一区

公告:魔扣目錄網(wǎng)為廣大站長提供免費收錄網(wǎng)站服務(wù),提交前請做好本站友鏈:【 網(wǎng)站目錄:http://www.430618.com 】, 免友鏈快審服務(wù)(50元/站),

點擊這里在線咨詢客服
新站提交
  • 網(wǎng)站:51998
  • 待審:31
  • 小程序:12
  • 文章:1030137
  • 會員:747

Laravel使用Observer(觀察者)實現(xiàn)日志管理模塊

寫在前面:

這里實現(xiàn)日志管理寫了兩篇,第一篇是簡單的模型增刪改監(jiān)聽并記錄日志。第二篇主要介紹的是通過導(dǎo)入文件進行批量數(shù)據(jù)處理無法很好的被監(jiān)聽處理到,這一部分的數(shù)據(jù)處理邏輯如何被記錄下來。詳細請看Laravel日志管理記錄導(dǎo)入文件后的數(shù)據(jù)變化。

1、創(chuàng)建observer文件,我這里是要記錄倉庫庫存模塊的操作日志,所以執(zhí)行下面的語句,會在app/Observers下面創(chuàng)建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、監(jiān)聽該模塊下的增刪改操作,這里使用Repository當(dāng)然也可以直接使用model。created、updated、deleted分別監(jiān)聽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;
    }
 
    //創(chuàng)建
    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']);
            //拼接數(shù)據(jù)
            $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']);
                }
                //拼接數(shù)據(jù)
                $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']);
            //拼接數(shù)據(jù)
            $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、數(shù)據(jù)庫


60eb21eb75c9b.png


分享到:
標(biāo)簽:Laravel Observer 日志管理模塊
用戶無頭像

網(wǎng)友整理

注冊時間:

網(wǎng)站:5 個   小程序:0 個  文章:12 篇

  • 51998

    網(wǎng)站

  • 12

    小程序

  • 1030137

    文章

  • 747

    會員

趕快注冊賬號,推廣您的網(wǎng)站吧!
最新入駐小程序

數(shù)獨大挑戰(zhàn)2018-06-03

數(shù)獨一種數(shù)學(xué)游戲,玩家需要根據(jù)9

答題星2018-06-03

您可以通過答題星輕松地創(chuàng)建試卷

全階人生考試2018-06-03

各種考試題,題庫,初中,高中,大學(xué)四六

運動步數(shù)有氧達人2018-06-03

記錄運動步數(shù),積累氧氣值。還可偷

每日養(yǎng)生app2018-06-03

每日養(yǎng)生,天天健康

體育訓(xùn)練成績評定2018-06-03

通用課目體育訓(xùn)練成績評定