Skip to content

Conversation

wimski
Copy link

@wimski wimski commented Jul 3, 2025

This feature makes the following use case with custom model event classes possible.

class MyModel extends Model
{
    use PivotEventTrait;

    /**
     * @var array<string, class-string>
     */
    protected $dispatchesEvents = [
        'pivotSynced' => MyEvent::class,
    ];
}
readonly class MyEvent
{
    /**
     * @param array{
     *     model: Model,
     *     relation: string,
     *     pivotIds: array<array-key, int|string>,
     *     pivotIdsAttributes: array<int|string, array<string, mixed>>,
     * } $payload
     */
    public function __construct(
        public MyModel $model,
        public array $payload,
    ) {
    }
}
readonly class MyListener
{
    public function __invoke(MyEvent $event): void
    {
         // $event->model
         // $event->payload
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant