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

How Laravel uses Observer to implement log management module

藏色散人
Release: 2021-07-12 08:55:32
forward
2281 people have browsed it

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
Copy after login

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

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

Enable observer under App\Providers\AppServiceProvider

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

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);
        }
    }}
Copy after login

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!

Related labels:
source:csdn.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template