博主信息
冷雨画桥
博文
7
粉丝
0
评论
0
访问量
927
积分:0
P豆:14.5

Laravel Observer (ORM 观察器)

2020年06月28日 16:08:05阅读数:174博客 / 冷雨画桥/ Laravel -- 从头开始

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. ?>

全部评论

文明上网理性发言,请遵守新闻评论服务协议

条评论
暂无评论暂无评论!