Blogger Information
Blog 7
fans 0
comment 0
visits 15431
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
Laravel Observer (ORM 观察器)
冷雨画桥
Original
1434 people have browsed it

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. ?>
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post