LaravelでのDB操作を監視する

 LaravelでのDB操作を監視する

2020-05-16に開催された【オンライン】関西PHP勉強会で発表したスライドです。 #phpkansai

A73ed5cefbbd84abd2c5e14a5eb4a339?s=128

Ippei Sumida

May 16, 2020
Tweet

Transcript

  1. LaravelͰͷDBૢ࡞Λ ؂ࢹ͢Δ ʲΦϯϥΠϯʳؔ੢PHPษڧձ 2020-05-16 Ippei Sumida

  2. # WHOAMI - ֯ాɹҰฏʢ͢Έͩɹ͍ͬ΃͍ʣ - ϑϦʔϥϯεσϕϩούʔ - PHPͳͲͰWeb։ൃͯ͠·͢ - ຕํࢢͷίϫʔΩϯάεϖʔε


    ʰSHINCRUʱͷӡӦελοϑͯ͠·͢ - CoderDojoຕํ΍ͬͯ·͢
  3. ʮԿ΋ͯ͠ͳ͍ͷʹյΕͨʯ

  4. ͋ɺσʔλ࡟আ͞Εͯ·͢Ͷʔ ˎˎͷσʔλ͕දࣔ͞Εͯͳ͍ΜͰ͚͢Ͳʁ ͑ʂͳʹ΋ͯ͠ͳ͍Ͱ͢ΑʂʂόάͰ͔͢ʁʁʁ ͍΍ɺγεςϜతʹ͸ਖ਼ৗʹಈT ͪΌΜͱௐࠪͯ͠ใࠂॻग़͍ͯͩ͘͠͞ʂʂ

  5. ͏ͪͷ#͞Μ͕࡟আͨ͠Μͩͬͯ❤

  6. ୭͕ԿΛ͔ͨ͠؂ࢹ͍ͨ͠

  7. Laravel Auditing

  8. Laravel AuditingͰͰ͖Δ͜ͱ Laravel্ͷ… • DBૢ࡞ͷ؂ࢹ • ؂ࢹσʔλͷऔಘ • ͩΕ͕ɾ͍ͭɾͲ͜ͰɾͳʹΛ͔ͨ͠Λ೺Ѳ •

    ߋ৽ͷࠩ෼ʢͳʹΛͲ͏ม͔͑ͨʣΛ೺Ѳ
  9. Πϯετʔϧ composer require owen-it/laravel-auditing

  10. ઃఆ1 ‘providers’ => [ + OwenIt\Auditing\AuditingServiceProvider::class ], # config/audit.phpੜ੒ php

    artisan vendor:publish —provider \ ”OwenIt\Auditing\AuditingServiceProvider” \ --tag="config" # ϚΠάϨʔγϣϯʢauditsςʔϒϧCREATEʣੜ੒ php artisan vendor:publish —provider \ “OwenIt\Auditing\AuditingServiceProvider" \ -—tag=“migrations" ˌϚΠάϨʔγϣϯ php artisan migrate DPOpHBQQQIQ
  11. ઃఆ2 <?php namespace App; use Illuminate\Database\Eloquent\Model; + use OwenIt\Auditing\Contracts\Auditable; +

    + class Item extends Model implements Auditable - class Item extends Model { + use \OwenIt\Auditing\Auditable; BQQ*UFNQIQ
  12. ؂ࢹελʔτ

  13. id user_ty pe user_id event auditabl e_type auditabl e_id old_val

    ues new_val ues url ip_addr ess user_ag ent tags created _at updated _at 1 App\Use r 1 updated App\Item 2 {"name": "\u8ffd\u 52a01"} {"name": "\u8ffd\u 52a02"} http:// localhost :8000/ items/2 127.0.0. 1 Mozilla/ 5.0 (Macinto sh; Intel Mac OS X 10_15_3 NULL 2020-03- 23 12:20:34 2020-03- 23 12:20:34 2 App\Use r 1 created App\Item 3 [] {"name": "\u307b\ u3052"," stock":"1 0","id":3} http:// localhost :8000/ items 127.0.0. 1 Mozilla/ 5.0 (Macinto sh; Intel Mac OS X 10_15_3 NULL 2020-03- 23 12:21:39 2020-03- 23 12:21:39 3 App\Use r 1 deleted App\Item 3 {"id":"3"," name":"\ u307b\u 3052","st ock":"10" } [] http:// localhost :8000/ items/3 127.0.0. 1 Mozilla/ 5.0 (Macinto sh; Intel Mac OS X 10_15_3 NULL 2020-03- 23 12:51:57 2020-03- 23 12:51:57
  14. ϓϩάϥϜͰ औಘ <?php $item = Item::find(1); // ֘౰σʔλͷશͯͷ؂ࢹσʔλ $audits =

    $item->audits; // ֘౰σʔλͷ࠷ॳͷ؂ࢹσʔλ $first = $item->audits()->first(); // ֘౰σʔλͷ௚ۙͷ؂ࢹσʔλ $latest = $item->audits()->latest()->first(); // ֘౰σʔλͷ؂ࢹσʔλͷIDࢦఆ $audit = $item->audits()->find(1);
  15. ͩΕ͕ ԿΛͨ͠ ͞ΘͬͨϞσϧ ͞Θͬͨσʔλ ͼ;͒ʔ ͋;ͨʔ 1 updated App\Item 2

    {"name":"\u8ffd\u52a 01"} {"name":"\u8ffd\u52a 02"}
  16. Ͳ͜Ͱ ͲͷIPΞυϨεͰ Ͳͷϒϥ΢βͰ ͍ͭ http://localhost:8000/items/2 127.0.0.1 Mozilla/5.0 (Macintosh; Intel Mac

    OS X 10_15_3) AppleWebKit/ 605.1.15 (KHTML, like Gecko) Version/13.0.5 Safari/605.1.15 2020-03-23 12:20:34
  17. ͋ɺσʔλ࡟আ͞Εͯ·͢Ͷʔ ˎˎͷσʔλ͕දࣔ͞Εͯͳ͍ΜͰ͚͢Ͳʁ ͑ʂͳʹ΋ͯ͠ͳ͍Ͱ͢ΑʂʂόάͰ͔͢ʁʁʁ #͞Μ͕ ೔ͷ ࣌͝ΖʹεϚϗͰ࡟আͯ͠·͢Ͷɻ ͦ͏Ͱ͔͢ɻ#͞Μʹฉ͍ͯΈ·͢ʂ

  18. એ఻

  19. Symfony Meetup Kansai ΍ͬͯ·͢

  20. Symfony Meetup Kansai͸͜Μͳͱ͜Ζ • Symfonyʹ͍ͭͯࡶஊ͢ΔձʢݱࡏΦϯϥΠϯ։࠵ʣ • Symfonyͷܦݧ໰Θͣ • SymfonyͰʙͭͬͨ͘ɺʙͬͯػೳ͔ͭͬͯΈͨɺͱ͍͏ࣄྫ঺հ΍ ։ൃख๏ͳͲΛ৘ใڞ༗

    • ࣍͸6݄͔7݄͘Β͍ʹ։࠵༧ఆ
  21. ͋Γ͕ͱ͏͍͟͝·ͨ͠