博客列表 >Laravel Observer (ORM 观察器)

Laravel Observer (ORM 观察器)

冷雨画桥
冷雨画桥原创
2020年06月28日 16:08:051436浏览

Observer

  1. //注册观察器
  2. App\Providers\AppServiceProvider.php;
  3. class AppServiceProvider extends ServiceProvider
  4. {
  5. public function boot(){
  6. $this->addModelLog();
  7. }
  8. public function addModelLog(){
  9. User::observe(Observer::class);
  10. }
  11. }
  12. App\Observers\Observer.php
  13. <?php
  14. namespace App\Observers;
  15. use Illuminate\Database\Eloquent\Model;
  16. use App\Jobs\OrmObserver;
  17. class Observer{
  18. 插入前
  19. public function creating(Model $model){
  20. }
  21. //插入后
  22. public function created(Model $model){
  23. }
  24. //修改前
  25. public function updating(Model $model){
  26. }
  27. //修改后
  28. public function updated(Model $model){
  29. }
  30. //删除前
  31. public function deleting(Model $model){
  32. }
  33. //删除后
  34. public funtion deleted(Model $model){
  35. }
  36. public funtion ObserverJob(){
  37. OrmObserver::dispatch(json_encode($data))->onConnection('redis')->onQueue('ObserverQueue');
  38. }
  39. }
  40. ?>
  41. App\Job\OrmObserver;
  42. <?php
  43. namespace App\Jobs;
  44. use Illuminate\Bus\Queueable;
  45. use Illuminate\Contracts\Queue\ShouldQueue;
  46. use Illuminate\Foundation\Bus\Dispatchable;
  47. use Illuminate\Queue\InteractsWithQueue;
  48. use Illuminate\Queue\SerializesModels;
  49. use Illuminate\Support\Facades\Log;
  50. use Modules\Crm\Models\CrmOrmSqlLog;
  51. class OrmObserver implements ShouldQueue
  52. {
  53. use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
  54. protected $data = [];
  55. public function __construct($data){
  56. $this->data = json_decode($data)
  57. }
  58. public function handle(){
  59. }
  60. ?>
声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议