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、数据库
Laravel使用Observer(观察者)的更多相关文章
- java: 观察者模式:Observable被观察者,Observer观察者
java: 观察者模式:Observable被观察者,Observer观察者 以房子价格为例,卖房者为被观察者: import java.util.Observable; //被观察者子类化 publ ...
- Java Observer 观察者
http://www.cnblogs.com/jaward/p/3277619.html 1.API 被观察者 java.util.Observable; public class Observabl ...
- Java设计模式——Observer(观察者)模式
在多个对象之间建立一对多的关系,以便当一个对象状态改变的时候.其它全部依赖于这个对象的对象都能得到通知,并被自己主动更新. 适用情况: 当一个抽象模型有两个方面,当中一个方面依赖于还有一方面. 将这二 ...
- 十七、 Observer 观察者设计模式
设计: 代码清单: Observer public interface Observer { void update(NumberGenerator generator); } DigitObserv ...
- Laravel 利用 observer 类基于状态属性,对进行删除和修改的控制
1 我们知道 Observer 类可以监听模型类的相关事件 1.1 creating, created, updating, updated, saving, saved, deleting, del ...
- Observer观察者设计模式
Observer设计模式主要包括以下两种对象: (1)被观察对象:Subject,它往往包含其他对象感兴趣的东西,上面例子中热水器中就是Subject(被监视对象); (2)观察对象:Observer ...
- Observer 观察者
意图 定义对象间的一种一对多的依赖关系 ,当一个对象的状态发生改变时, 所有依赖于它的对象都得到通知并被自动更新. 动机 一致性,松耦合 需要维护相关对象间的一致性.我们不希望为了维持一致性而使各类紧 ...
- Observer(观察者)设计模式[转]
Observer设计模式中主要包括如下两类对象: Subject:监视对象,它往往包含着其他对象所感兴趣的内容.在本范例中,热水器就是一个监视对象,它包含的其他对象所感兴趣的内容,就是tempratu ...
- C#委托与事件之观察者Observer设计模式
前言 委托: 委托是一种在对象里保存方法引用的类型,同时也是一种类型安全的函数指针. 或委托可以看成一种表示函数的数据类型,类似函数指针. 事件是特殊的委托 观察者模式:两种角色:(1)Subj ...
随机推荐
- python基础之迭代器、生成器、装饰器
一.列表生成式 a = [0,1,2,3,4,5,6,7,8,9] b = [] for i in a: b.append(i+1) print(b) a = b print(a) --------- ...
- Centos7.4永久修改系统时间
[root@V3B01-zsy yum.repos.d]# date -s "2019-09-24 17:02:30" 2019年 09月 24日 星期二 17:02:30 CST ...
- sentinel入门
sentinel下载:sentinel-dashboard-1.8.1.jar 下载完成后进入sentinel-dashboard-1.8.1.jar的文件夹,在cmd中输入java -jar sen ...
- TVM性能评估分析(五)
TVM性能评估分析(五) Figure 3. A futher speed up with operator fusion Table 1. Performance issue of cuBLAS ...
- cmodel模拟器开发
cmodel模拟器开发 对于一个公司来说,产品的设计周期就是生命线,一般来说都会在设计功能级仿真的c-model后直接转向RTL设计. 在目前的技术下,做cycle-by-cycle的设计和直接RTL ...
- 使用现代C++如何避免bugs(下)
使用现代C++如何避免bugs(下) About virtual functions Virtual functions hinder a potential problem: the thing ...
- 操作系统-Linux命令
一.目录结构 #因为根目录与开机有关,开机过程中仅有根目录会被挂载, 因此根目录下与开机过程有关的目录(以下5个),不能与根目录放到不同的分区去. /etc:配置文件 /dev:所需要的装置文件 /l ...
- Kubernetes 实战——升级应用(Deployment)
一.更新运行在 Pod 内的应用程序 1. 修改 Pod 模板 将导致应用程序在一定时间内不可用 2. 修改 Service 的 Pod 选择器 需要同时运行两倍的 Pod 3. 滚动升级 应用程序需 ...
- UF_LAYER 图层操作
Open C uc5007uc5008uc5009UF_LAYER_ask_category_infoUF_LAYER_ask_category_tagUF_LAYER_ask_statusUF_LA ...
- Spring Boot WebFlux-02——WebFlux Web CRUD 实践
第02课:WebFlux Web CRUD 实践 上一篇基于功能性端点去创建一个简单服务,实现了 Hello.这一篇用 Spring Boot WebFlux 的注解控制层技术创建一个 CRUD We ...