Pckg Documentation
Pckg Documentation
  • About
  • Concept
  • Start
  • Deployment
  • Tests
  • Ecosystem
    • Docker images
    • Skeleton
  • Core packages
    • Auth
    • Cache
    • Collection
    • Concept
    • Database
      • Repository
        • Driver
      • Entity
        • Relations
        • Query
      • Record
        • Fields
        • Model
      • Events
      • Extensions
    • Framework
      • Environment
        • Console
        • Development
        • Production
        • Test
      • Application
      • Providers
      • Config
      • Router
      • Request
      • Response
      • Controller
        • Middleware
        • Afterware
      • View
      • Events
      • Exception
    • Generic
      • Dynamic
      • Generic
      • Maestro
    • Htmlbuilder
      • Elements
        • Form
        • Fields
      • Datasources
      • Validators
      • Decorators
    • Locale
    • Mail
      • Drivers
      • Template
      • Mail
    • Manager
      • Asset manager
      • SEO manager
      • Meta manager
      • Upload manager
      • Vue manager
      • Job manager
      • Locale manager
      • Page manager
    • Migrator
      • Migrations
      • Fields
    • Queue
      • Driver
      • Publisher
      • Subscriber
    • Storage
      • Driver
      • Media
    • Translator
  • More packages
    • API
    • HTTP QL
      • Read
      • Write
      • Uploads
    • Task
      • Async
    • Websocket
      • Server
      • Client
  • Frontend
    • Helpers JS
    • Helpers CSS
  • Extras
    • Parser
    • Payment
    • Tenant
Powered by GitBook
On this page

Was this helpful?

  1. Core packages
  2. Database

Events

Across the code, there are different events allowing you to subscribe to them:

  • Record::class.inserting

  • Record::class.inserted

  • Record::class.updating

  • Record::class.updated

  • Record::class.deleting

  • Record::class.deleted

    • static::class... - all global Record::class events also have their local static::class events, for example User::class.inserting

Adding a handler

You can register a handler in any of your providers:

<?php

namespace Vendor\Domain\Provider;

use Pckg\Framework\Provider;
use Vendor\Domain\Record\User;
use Vendor\Domain\Handler\LogUserInserted;

class MyProvider extends Provider
{
    public function listeners()
    {
        return [
            User::class . '.inserted' => [
                LogUserInserted::class,
            ],
        ];
    }
}

And then implement your event handler:

<?php

namespace Vendor\Domain\Handler;

class LogUserInserted
{
    public function __construct(protected User $user) { }

    public function handle()
    {
        error_log('User ' . $this->user->id . ' inserted.');
    }
}
PreviousModelNextExtensions

Last updated 2 years ago

Was this helpful?