登录  /  注册
首页 > php框架 > Laravel > 正文

Laravel如何使用Observer实现日志管理模块

藏色散人
发布: 2021-07-10 15:27:46
转载
2007人浏览过

Laravel使用Observer(观察者)实现日志管理模块

写在前面:
这里实现日志管理写了两篇,第一篇是简单的模型增删改监听并记录日志。第二篇主要介绍的是通过导入文件进行批量数据处理无法很好的被监听处理到,这一部分的数据处理逻辑如何被记录下来。详细请看laravel日志管理记录导入文件后的数据变化。

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

<?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、数据库
在这里插入图片描述

相关推荐:最新的五个Laravel视频教程

以上就是Laravel如何使用Observer实现日志管理模块的详细内容,更多请关注php中文网其它相关文章!

智能AI问答
PHP中文网智能助手能迅速回答你的编程问题,提供实时的代码和解决方案,帮助你解决各种难题。不仅如此,它还能提供编程资源和学习指导,帮助你快速提升编程技能。无论你是初学者还是专业人士,AI智能助手都能成为你的可靠助手,助力你在编程领域取得更大的成就。
相关标签:
来源:csdn网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
作者最新文章
最新问题
关于CSS思维导图的课件在哪? 课件
凡人来自于2024-04-16 10:10:18
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2024 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号