Skip to main content

AutoDialerCampaignCallerId Model

Overview

Pivot model for the many-to-many relationship between campaigns and Caller IDs (DIDs). Stores the Caller ID pool configuration including optional weights.

Table: auto_dialer_campaign_caller_ids
Model: App\Models\AutoDialerCampaignCallerId
Type: Eloquent Pivot

Database Schema

ColumnTypeNullableDefaultDescription
idbigint unsignedNoautoPrimary key
campaign_idbigint unsignedNo-FK to auto_dialer_campaigns
did_number_idbigint unsignedNo-FK to did_numbers
weightint unsignedNo1Distribution weight
created_attimestampYesnullCreation timestamp
updated_attimestampYesnullLast update timestamp

Indexes

  • Primary: id
  • Unique: campaign_id + did_number_id (prevents duplicate assignments)
  • Foreign Keys: Indexed automatically

Relationships

Belongs To

$pivot->campaign() → AutoDialerCampaign
$pivot->didNumber() → DidNumber

Usage

Assigning Caller IDs to Campaign

use App\Models\AutoDialerCampaign;

$campaign = AutoDialerCampaign::find(1);

// Method 1: Using sync (recommended)
$campaign->callerIds()->sync([
1 => ['weight' => 1], // did_id => [pivot_data]
2 => ['weight' => 1],
3 => ['weight' => 2], // Higher weight = more frequent selection
]);

// Method 2: Using attach
$campaign->callerIds()->attach($didId, ['weight' => 1]);

// Method 3: Using detach/remove
$campaign->callerIds()->detach($didId);

Accessing Pivot Data

// When iterating caller IDs
foreach ($campaign->callerIds as $did) {
$weight = $did->pivot->weight;
echo "{$did->phone_number} has weight {$weight}";
}

Updating Weights

// Update weight for specific DID in campaign
$campaign->callerIds()->updateExistingPivot($didId, [
'weight' => 3
]);

Weight System

The weight field controls distribution frequency in weighted strategies:

WeightMeaningExample (3 DIDs with weights 1,1,2)
1Normal frequency25% selection rate
22x frequency50% selection rate
33x frequencyHigher selection rate

Note: While the backend supports weights, the current UI presents a simplified view without weight editing. All Caller IDs are assigned weight=1 by default.

Constraints

  • Maximum 100 Caller IDs per campaign (enforced at application level)
  • Only DIDs with status = 'active' can be assigned
  • DIDs must belong to the same organization as the campaign

Validation

// In CreateCampaignRequest/UpdateCampaignRequest
'caller_id_pool' => ['required', 'array', 'min:1', 'max:100'],
'caller_id_pool.*.did_id' => ['required', 'exists:did_numbers,id'],
'caller_id_pool.*.weight' => ['sometimes', 'integer', 'min:1', 'max:100'],

Example Queries

// Get all campaigns using a specific DID
$campaigns = AutoDialerCampaign::whereHas('callerIds', function ($q) use ($didId) {
$q->where('did_numbers.id', $didId);
})->get();

// Get count of Caller IDs per campaign
$counts = AutoDialerCampaign::withCount('callerIds')->get();

// Check if DID is in campaign pool
$hasDid = $campaign->callerIds()->where('did_numbers.id', $didId)->exists();