Skip to main content

Class

Opscale\NotificationCenter\Models\Audience Extends Illuminate\Database\Eloquent\Model. Uses AudienceRepository, HasUlids, SoftDeletes, and ValidatorTrait. Database table: notification_center_audiences

Attributes

id
string
required
ULID primary key, auto-generated on creation.
name
string
required
Human-readable name for the audience (e.g. "Premium subscribers").
description
string
required
Longer description explaining the purpose or composition of this audience.
type
AudienceType
required
Determines how the member profiles are resolved. Cast to the AudienceType enum.
criteria
string | array | null
Resolution criteria whose meaning depends on type:
  • Static — not used; members are managed via the pivot table directly.
  • Dynamic — a raw SQL query string that returns rows with an id column corresponding to profile IDs.
  • Segment — a comma-separated string of tag names used to match profiles via Profile::withAnyTags().
Stored as text in the database; cast to array by Eloquent.
total_members
integer
Cached count of the resolved profiles. Updated automatically by getProfiles() for Dynamic and Segment audiences. Defaults to 0.
created_at
Carbon\Carbon | null
Timestamp of when the record was created.
updated_at
Carbon\Carbon | null
Timestamp of the last update.
deleted_at
Carbon\Carbon | null
Soft-delete timestamp. Present when the record has been soft-deleted.

Enums

AudienceType

Opscale\NotificationCenter\Models\Enums\AudienceType
ValueStringDescription
STATIC'Static'A fixed, manually-curated list of profiles. Members are managed through the notification_center_audience_profile pivot table.
DYNAMIC'Dynamic'Membership is resolved at dispatch time by executing the raw SQL stored in criteria. Any profile whose ID is returned by the query is included.
SEGMENT'Segment'Membership is resolved by matching profile tags. The criteria field holds a comma-separated list of tag names; profiles with any matching tag are included.

Relationships

profiles

Returns BelongsToMany — the Profile records explicitly attached to this audience (relevant for Static audiences). The pivot table is notification_center_audience_profile.
$audience->profiles; // Illuminate\Database\Eloquent\Collection<Profile>

notifications

Returns BelongsToMany — the Notification records that target this audience via the notification_center_audience_notification pivot table.
$audience->notifications; // Illuminate\Database\Eloquent\Collection<Notification>

Repository pattern

The AudienceRepository trait mixed into this model provides the getProfiles() method, which resolves the correct set of profiles based on the audience type.

getProfiles()

Returns Illuminate\Database\Eloquent\Collection<Profile> — the resolved profiles for this audience.
$profiles = $audience->getProfiles();
Internally, the method delegates to one of three private helpers depending on the audience type:
Dynamic audiences execute a raw SQL string stored in the database. Ensure that only trusted administrators can create or modify Dynamic audiences to avoid SQL injection risk.
total_members is a cached value and is only refreshed when getProfiles() is called for Dynamic and Segment types. For Static audiences it is not automatically maintained — update it manually if you need an accurate count.

Usage examples

Creating a Static audience

use Opscale\NotificationCenter\Models\Audience;
use Opscale\NotificationCenter\Models\Enums\AudienceType;

$audience = Audience::create([
    'name'        => 'VIP customers',
    'description' => 'Hand-picked high-value customers.',
    'type'        => AudienceType::STATIC,
]);

// Attach profiles manually
$audience->profiles()->attach([$profileA->id, $profileB->id]);

Creating a Segment audience

$audience = Audience::create([
    'name'        => 'Beta testers',
    'description' => 'All profiles tagged as beta-tester.',
    'type'        => AudienceType::SEGMENT,
    'criteria'    => 'beta-tester',
]);

// Resolve members at dispatch time
$profiles = $audience->getProfiles();

Creating a Dynamic audience

$audience = Audience::create([
    'name'        => 'New users (last 30 days)',
    'description' => 'Profiles created in the past 30 days.',
    'type'        => AudienceType::DYNAMIC,
    'criteria'    => "SELECT id FROM notification_center_profiles WHERE created_at >= DATE_SUB(NOW(), INTERVAL 30 DAY)",
]);

$profiles = $audience->getProfiles(); // executes the raw SQL
echo $audience->total_members; // updated automatically

Build docs developers (and LLMs) love