# Campaign Email Model
| DB Table Name | {wp_db_prefix}_fc_campaign_emails |
|---------------|------------------------------------------------------------------------------|
| Schema | Check Schema |
| Source File | fluent-crm/app/Models/CampaignEmail.php |
| Name Space | FluentCrm\App\Models |
| Class | FluentCrm\App\Models\CampaignEmail |
## Attributes
| Attribute |
Data Type |
Comment |
| id |
Integer |
|
| campaign_id |
Integer |
|
| email_type |
String |
|
| subscriber_id |
Integer |
|
| email_subject_id |
Integer |
|
| email_address |
String |
|
| email_subject |
String |
|
| email_body |
Text |
|
| email_headers |
Text |
|
| is_open |
Boolean |
|
| is_parsed |
Boolean |
|
| click_counter |
Integer |
|
| status |
String |
|
| note |
Integer |
|
| scheduled_at |
Date Time |
|
| email_hash |
String |
|
| created_at |
Date Time |
|
| updated_at |
Date Time |
|
## Usage
Please check Model Basic for Common methods.
### Accessing Attributes
```php
$campaignEmail = FluentCrm\App\Models\CampaignEmail::find(1);
$campaignEmail->id; // returns id
$campaignEmail->email; // returns email
.......
```
## Relations
This model has the following relationships that you can use
### campaign
Access the associated campaign of a model
- return `FluentCrm\App\Models\Campaign` Model Collection
#### Example:
```php
// Accessing Template
$campaign = $campaignEmail->campaign;
// For Filtering by template relationship
// Get CampaignEmails which has type: funnel_email_campaign
$campaignEmails = FluentCrm\App\Models\CampaignEmail::whereHas('campaign', function($query) {
$query->where('type', 'funnel_email_campaign');
})->get();
// Get CampaignEmails which does not have type: funnel_email_campaign
$campaignEmails = FluentCrm\App\Models\CampaignEmail::whereDoesntHave('campaign', function($query) {
$query->where('type', 'funnel_email_campaign');
})->get();
```
### subscriber
Access all the associated subscriber of a model
- return `FluentCrm\App\Models\Subscriber` Model Collections
#### Example:
```php
// Accessing Subscriber
$subscriber = $campaignEmail->subscriber;
// For Filtering by tags relationship
// Get CampaignEmails which has first_name Demo
$campaignEmails = FluentCrm\App\Models\CampaignEmail::whereHas('subscriber', function($query) {
$query->where('first_name', 'Demo');
})->get();
// Get CampaignEmails which does not have first_name Demo
$campaignEmails = FluentCrm\App\Models\CampaignEmail::whereDoesntHave('subscriber', function($query) {
$query->where('first_name', 'Demo');
})->get();
```
### subject
Access all the associated subject of a model
- return `FluentCrm\App\Models\Subject` Model Collections
#### Example:
```php
// Access all the associated subject of a model
$subject = $campaignEmail->subject;
```
## Methods
Along with Global Model methods, this model has few helper methods.
### markAs($status)
Update campaign email status
- Parameters
- $status `string`
- Returns `FluentCrm\App\Models\CampaignEmail`
#### Usage
```php
$campaignEmail = $campaignEmail->markAs('sent');
```
### markAsSent()
Update campaign email status to 'sent'
- Parameters
- none
- Returns `FluentCrm\App\Models\CampaignEmail`
#### Usage
```php
$campaignEmail = $campaignEmail->markAsSent();
```
### markAsFailed()
Update campaign email status to 'failed'
- Parameters
- none
- Returns `FluentCrm\App\Models\CampaignEmail`
#### Usage
```php
$campaignEmail = $campaignEmail->markAsFailed();
```
### data()
Get data for the email to be sent
- Parameters
- none
- Returns `array`
#### Usage
```php
$emailData = $campaignEmail->data();
```
### previewData()
Preview data for the email to be sent
- Parameters
- none
- Returns `array`
#### Usage
```php
$emailData = $campaignEmail->previewData();
```
### getEmailSubject()
Get the subject of email
- Parameters
- none
- Returns `string`
#### Usage
```php
$subject = $campaignEmail->getEmailSubject();
```
### getEmailBody()
Get the email body (actual html code) of email
- Parameters
- none
- Returns `text`
#### Usage
```php
$emailBody = $campaignEmail->getEmailBody();
```
### getCampaignUrls()
Get the urls which are shared in an email model
- Parameters
- none
- Returns `array` list of urls
#### Usage
```php
$urls = $campaignEmail->getCampaignUrls();
```
### getClicks()
Get the click counts of an email
- Parameters
- none
- Returns `array`
#### Usage
```php
$totalClicks = $campaignEmail->getClicks();
```
### getSubjectCount($campaignId)
Get the total subject of a campaign of email
- Parameters
- $campaignId `int`
- Returns `object`
#### Usage
```php
$result = $campaignEmail->getSubjectCount(1);
```
### getOpenCount()
Get the open count of an email
- Parameters
- none
- Returns `int`
#### Usage
```php
$openCount = $campaignEmail->getOpenCount();
```