laravel 模型观察器 observer 模型数据变化 触发观察器事件
1.生成观察器,会在Observers 文件夹下生成PurchaseOrderProductObserver观察器文件
php artisan make:observer PurchaseOrderProductObserver -m Models/PurchaseOrderProduct
<?php namespace App\Observers; use App\Models\PurchaseOrderProduct; use App\Models\ShipmentProduct; use Illuminate\Support\Facades\Log; class PurchaseOrderProductObserver { /** * Handle the purchase order product "created" event. * * @param \App\Models\PurchaseOrderProduct $purchaseOrderProduct * @return void */ public function created(PurchaseOrderProduct $purchaseOrderProduct) { // } /** * Handle the purchase order product "updated" event. * * @param \App\Models\PurchaseOrderProduct $purchaseOrderProduct * @return void */ public function updated(PurchaseOrderProduct $purchaseOrderProduct) { // Log::info('observers', [$purchaseOrderProduct->price]); //isDirty 验证模型里某字段是否被改变 if($purchaseOrderProduct->isDirty('price')){ ShipmentProduct::where('purchase_order_product_id', '=', $purchaseOrderProduct->id)->update(array( 'supplier_price'=> $purchaseOrderProduct->price, )); } else { Log::info('observers', ['isDirty']); } } /** * Handle the purchase order product "deleted" event. * * @param \App\Models\PurchaseOrderProduct $purchaseOrderProduct * @return void */ public function deleted(PurchaseOrderProduct $purchaseOrderProduct) { // } /** * Handle the purchase order product "restored" event. * * @param \App\Models\PurchaseOrderProduct $purchaseOrderProduct * @return void */ public function restored(PurchaseOrderProduct $purchaseOrderProduct) { // } /** * Handle the purchase order product "force deleted" event. * * @param \App\Models\PurchaseOrderProduct $purchaseOrderProduct * @return void */ public function forceDeleted(PurchaseOrderProduct $purchaseOrderProduct) { // } }
2.在Providers/AppServiceProvider 注册观察器
<?php namespace App\Providers; use App\Models\PurchaseOrderProduct; use App\Observers\PurchaseOrderProductObserver; use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\Schema; use Illuminate\Support\ServiceProvider; class AppServiceProvider extends ServiceProvider { /** * Register any application services. * * @return void */ public function register() { // Schema::defaultStringLength(191); //左侧菜单 view()->composer('erp.layout',function($view){ $menus = \App\Models\Permission::with([ 'childs'=>function($query){$query->with('icon')->orderBy('sort','desc');} ,'icon'])->where('parent_id',0)->orderBy('sort','desc')->get(); $view->with('menus',$menus); }); // 如果要放入日志文件中 DB::listen( function ($sql) { foreach ($sql->bindings as $i => $binding) { if ($binding instanceof \DateTime) { $sql->bindings[$i] = $binding->format('\'Y-m-d H:i:s\''); } else { if (is_string($binding)) { $sql->bindings[$i] = "'$binding'"; } } } $query = str_replace(array('%', '?'), array('%%', '%s'), $sql->sql); $query = vsprintf($query, $sql->bindings); // Save the query to file $logFile = fopen( storage_path('logs' . DIRECTORY_SEPARATOR . date('Y-m-d') . '_query.log'), 'a+' ); fwrite($logFile, date('Y-m-d H:i:s') . ': ' . $query . PHP_EOL); fclose($logFile); } ); } /** * Bootstrap any application services. * * @return void */ public function boot() { //注入观察器 PurchaseOrderProduct::observe(PurchaseOrderProductObserver::class); } }
3.两种方式 保存数据都可以触发 观察器的update 方法
//第一种 update保存,必须加first PurchaseOrderProduct::where('id', '=', 1)->first()->update(array( 'label'=>'AAblocks1' )); //第二种 save保存 $purchaseOrderProduct = PurchaseOrderProduct::find(1); $purchaseOrderProduct->label = 'AAblocks2'; $purchaseOrderProduct->save();
4.laravel日志里可以看到打印的日志