這篇文章主要介紹“Laravel怎么使用Observer實(shí)現(xiàn)日志管理模塊”,在日常操作中,相信很多人在Laravel怎么使用Observer實(shí)現(xiàn)日志管理模塊問(wèn)題上存在疑惑,小編查閱了各式資料,整理出簡(jiǎn)單好用的操作方法,希望對(duì)大家解答”Laravel怎么使用Observer實(shí)現(xiàn)日志管理模塊”的疑惑有所幫助!接下來(lái),請(qǐng)跟著小編一起來(lái)學(xué)習(xí)吧!
專注于為中小企業(yè)提供成都網(wǎng)站建設(shè)、網(wǎng)站建設(shè)服務(wù),電腦端+手機(jī)端+微信端的三站合一,更高效的管理,為中小企業(yè)武山免費(fèi)做網(wǎng)站提供優(yōu)質(zhì)的服務(wù)。我們立足成都,凝聚了一批互聯(lián)網(wǎng)行業(yè)人才,有力地推動(dòng)了上千余家企業(yè)的穩(wěn)健成長(zhǎng),幫助中小企業(yè)通過(guò)網(wǎng)站建設(shè)實(shí)現(xiàn)規(guī)模擴(kuò)充和轉(zhuǎn)變。
寫(xiě)在前面:
這里實(shí)現(xiàn)日志管理寫(xiě)了兩篇,第一篇是簡(jiǎn)單的模型增刪改監(jiān)聽(tīng)并記錄日志。第二篇主要介紹的是通過(guò)導(dǎo)入文件進(jìn)行批量數(shù)據(jù)處理無(wú)法很好的被監(jiān)聽(tīng)處理到,這一部分的數(shù)據(jù)處理邏輯如何被記錄下來(lái)。詳細(xì)請(qǐng)看Laravel日志管理記錄導(dǎo)入文件后的數(shù)據(jù)變化。
1、創(chuàng)建observer文件,我這里是要記錄倉(cāng)庫(kù)庫(kù)存模塊的操作日志,所以執(zhí)行下面的語(yǔ)句,會(huì)在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下面開(kāi)啟observer
public function boot() { WarehouseInventory::observe(WarehouseInventoryObserver::class); }
2、監(jiān)聽(tīng)該模塊下的增刪改操作,這里使用Repository當(dāng)然也可以直接使用model。created、updated、deleted分別監(jiān)聽(tīng)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); } }}
到此,關(guān)于“Laravel怎么使用Observer實(shí)現(xiàn)日志管理模塊”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識(shí),請(qǐng)繼續(xù)關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編會(huì)繼續(xù)努力為大家?guī)?lái)更多實(shí)用的文章!
分享題目:Laravel怎么使用Observer實(shí)現(xiàn)日志管理模塊
URL地址:http://muchs.cn/article28/pjpocp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供虛擬主機(jī)、、建站公司、網(wǎng)站內(nèi)鏈、網(wǎng)站排名、外貿(mào)建站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)