Laravel使用Observer(觀察者)


 

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、數據庫

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM