Home  >  Article  >  PHP Framework  >  How Laravel uses Observer to implement log management module

How Laravel uses Observer to implement log management module

藏色散人
藏色散人forward
2021-07-10 15:27:462252browse

Laravel uses Observer to implement the log management module

Written in front:
I have written two articles here to implement log management. The first article is a simple model addition, deletion and modification monitoring and log recording. The second article mainly introduces how batch data processing through imported files cannot be well monitored and processed. How to record this part of the data processing logic. For details, please see Laravel Log Management to record data changes after importing files.

1. Create the observer file. I want to record the operation log of the warehouse inventory module, so executing the following statement will create the WarehouseInventoryObserver file under app/Observers.

php artisan make:observer WarehouseInventoryObserver --model=WarehouseInventory

Since the models are placed under app/Models, the path must be specified.

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

Enable observer under App\Providers\AppServiceProvider

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

2. Monitor the addition, deletion and modification operations under this module. Of course, you can use Repository here or you can use model directly. created, updated, and deleted respectively monitor the new, updated, and deleted operations of the WarehouseInventory model.

<?phpnamespace 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(&#39;name&#39;, &#39;id&#39;);
        $this->actionLogRepository = $actionLogRepository;
    }

    //创建
    public function created(WarehouseInventory $warehouseInventory)
    {
        if (!empty($this->user_id)) {
            $attributes = $warehouseInventory->getAttributes();
            $attributes = Arr::only($attributes, [&#39;warehouse_id&#39;, &#39;seller_sku&#39;, &#39;quantity&#39;, &#39;box&#39;]);
            $warehouse = $this->warehouse->get($attributes[&#39;warehouse_id&#39;]);
            //拼接数据
            $data = [
                &#39;module&#39; => &#39;warehouse_inventory&#39;,
                &#39;user_id&#39; => $this->user_id,
                &#39;type&#39; => &#39;create&#39;,
                &#39;content&#39; => [
                    &#39;warehouse&#39; => $warehouse,
                    &#39;seller_sku&#39; => $attributes[&#39;seller_sku&#39;],
                    &#39;original_quantity&#39; => 0,
                    &#39;current_quantity&#39; => $attributes[&#39;quantity&#39;],
                    &#39;box&#39; => $attributes[&#39;box&#39;]
                ]
            ];

            $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, [&#39;remark&#39;, &#39;updated_at&#39;]);
            if (count($dirty)) {
                if (Arr::has($dirty, &#39;warehouse_id&#39;)) {
                    $warehouse = $this->warehouse->get($dirty[&#39;warehouse_id&#39;]);
                } else {
                    $warehouse = $this->warehouse->get($original[&#39;warehouse_id&#39;]);
                }
                //拼接数据
                $data = [
                    &#39;module&#39; => &#39;warehouse_inventory&#39;,
                    &#39;user_id&#39; => $this->user_id,
                    &#39;type&#39; => &#39;update&#39;,
                    &#39;content&#39; => [
                        &#39;warehouse&#39; => $warehouse,
                        &#39;seller_sku&#39; => $original[&#39;seller_sku&#39;],
                        &#39;original_quantity&#39; => $original[&#39;quantity&#39;],
                        &#39;current_quantity&#39; => $dirty[&#39;quantity&#39;],
                        &#39;box&#39; => (Arr::has($dirty, &#39;box&#39;)) ? $dirty[&#39;box&#39;] : $original[&#39;box&#39;]
                    ]
                ];

                $this->actionLogRepository->makeModel()->create($data);
            }
        }
    }

    //删除
    public function deleted(WarehouseInventory $warehouseInventory)
    {
        if (!empty($this->user_id)) {

            $original = $warehouseInventory->getOriginal();
            $warehouse = $this->warehouse->get($original[&#39;warehouse_id&#39;]);
            //拼接数据
            $data = [
                &#39;module&#39; => &#39;warehouse_inventory&#39;,
                &#39;user_id&#39; => $this->user_id,
                &#39;type&#39; => &#39;delete&#39;,
                &#39;content&#39; => [
                    &#39;warehouse&#39; => $warehouse,
                    &#39;seller_sku&#39; => $original[&#39;seller_sku&#39;],
                    &#39;original_quantity&#39; => $original[&#39;quantity&#39;],
                    &#39;current_quantity&#39; => 0,
                    &#39;box&#39; => $original[&#39;box&#39;]
                ]
            ];

            $this->actionLogRepository->makeModel()->create($data);
        }
    }}

3. Database
How Laravel uses Observer to implement log management module

## related Recommended:
The latest five Laravel video tutorials

The above is the detailed content of How Laravel uses Observer to implement log management module. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete