Skip to content

[13.x] Support attribute inheritance for Broadcasting configuration in BroadcastsEvents#59583

Open
niduranga wants to merge 2 commits intolaravel:13.xfrom
niduranga:feature/attribute-inheritance
Open

[13.x] Support attribute inheritance for Broadcasting configuration in BroadcastsEvents#59583
niduranga wants to merge 2 commits intolaravel:13.xfrom
niduranga:feature/attribute-inheritance

Conversation

@niduranga
Copy link
Copy Markdown
Contributor


Currently, models using the BroadcastsEvents trait rely on class properties (e.g., $broadcastConnection, $broadcastQueue) or methods to define their broadcasting configuration. While Laravel has introduced Attribute-based configurations for other Eloquent features, Broadcasting lacked a dedicated attribute.

Furthermore, configuration defined on a BaseModel was not inherited by child models because the trait's reflection logic did not account for the inheritance chain.

Proposed Changes

  1. New Attribute: Introduced Illuminate\Database\Eloquent\Attributes\BroadcastsEvents to allow configuring connection, queue, and afterCommit via PHP 8 attributes.
  2. Inheritance Support: Updated the BroadcastsEvents trait to use Reflector::getClassAttribute() with ascend: true. This ensures that broadcasting settings defined on a parent class are automatically respected by all child models.
  3. Reflector Integration: Optimized the trait to resolve attributes using static::class to ensure reliable reflection during the inheritance lookup.

Benefits

  • DRY Principle: Define broadcasting configurations once in a base model instead of repeating properties in every child model.
  • Modern Syntax: Provides a declarative way to configure broadcasting, consistent with #[ObservedBy] and #[ScopedBy].

Example Usage

use Illuminate\Database\Eloquent\Attributes\BroadcastsEvents;

#[BroadcastsEvents(connection: 'redis', queue: 'high-priority', afterCommit: true)]
abstract class BaseModel extends Model
{
    use BroadcastsEvents;
}

class Order extends BaseModel
{
    // Automatically inherits broadcasting settings from BaseModel.
}

Testing

I have added an integration test in DatabaseEloquentBroadcastingTest.php to verify that:

  • The new #[BroadcastsEvents] attribute is correctly resolved.
  • Configuration is properly inherited from parent classes via the reflection ascend parameter.

@niduranga niduranga force-pushed the feature/attribute-inheritance branch 2 times, most recently from c0a483d to 9f51e7e Compare April 7, 2026 16:45
This commit introduces a new `BroadcastsEvents` attribute to allow
defining broadcasting configuration (connection, queue, afterCommit) 
via attributes. It also updates the `BroadcastsEvents` trait to 
support attribute inheritance using `Reflector::getClassAttribute()`.
@niduranga niduranga force-pushed the feature/attribute-inheritance branch from 9f51e7e to ba4091d Compare April 7, 2026 17:44
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