1. 程式人生 > 實用技巧 >cc分享java:Laravel Observer (ORM 觀察器)

cc分享java:Laravel Observer (ORM 觀察器)

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