您好,登錄后才能下訂單哦!
這篇文章主要介紹“Laravel怎么使用Observer實現(xiàn)日志管理模塊”,在日常操作中,相信很多人在Laravel怎么使用Observer實現(xiàn)日志管理模塊問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”Laravel怎么使用Observer實現(xiàn)日志管理模塊”的疑惑有所幫助!接下來,請跟著小編一起來學習吧!
寫在前面:
這里實現(xiàn)日志管理寫了兩篇,第一篇是簡單的模型增刪改監(jiān)聽并記錄日志。第二篇主要介紹的是通過導入文件進行批量數(shù)據(jù)處理無法很好的被監(jiān)聽處理到,這一部分的數(shù)據(jù)處理邏輯如何被記錄下來。詳細請看Laravel日志管理記錄導入文件后的數(shù)據(jù)變化。
1、創(chuàng)建observer文件,我這里是要記錄倉庫庫存模塊的操作日志,所以執(zhí)行下面的語句,會在app/Observers下面創(chuàng)建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、監(jiān)聽該模塊下的增刪改操作,這里使用Repository當然也可以直接使用model。created、updated、deleted分別監(jiān)聽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('name', 'id'); $this->actionLogRepository = $actionLogRepository; } //創(chuàng)建 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']); //拼接數(shù)據(jù) $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']); } //拼接數(shù)據(jù) $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']); //拼接數(shù)據(jù) $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); } }}
到此,關于“Laravel怎么使用Observer實現(xiàn)日志管理模塊”的學習就結束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學習,快去試試吧!若想繼續(xù)學習更多相關知識,請繼續(xù)關注億速云網站,小編會繼續(xù)努力為大家?guī)砀鄬嵱玫奈恼拢?/p>
免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經查實,將立刻刪除涉嫌侵權內容。