Viewing file: CampaignPerformance.php (1.67 KB) -rw-r--r-- Select action/file-type: (+) | (+) | (+) | Code (+) | Session (+) | (+) | SDB (+) | (+) | (+) | (+) | (+) | (+) |
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class CampaignPerformance extends Model { protected $fillable = [ 'campaign_id', 'date', 'spend', 'impressions', 'clicks', 'conversions', 'ctr', 'revenue', 'roi', 'country', 'device_type', 'hilltop_campaign_id', ];
protected $casts = [ 'spend' => 'decimal:2', 'ctr' => 'decimal:2', 'revenue' => 'decimal:2', 'roi' => 'decimal:2', 'date' => 'date', ];
/** * Scope a query to filter by date range */ public function scopeDateRange($query, $startDate, $endDate) { return $query->whereBetween('date', [$startDate, $endDate]); }
/** * Scope a query to filter by country */ public function scopeCountry($query, $country) { return $query->where('country', $country); }
/** * Scope a query to filter by device type */ public function scopeDeviceType($query, $deviceType) { return $query->where('device_type', $deviceType); }
/** * Calculate CTR if not already set */ public function calculateCtr() { if ($this->impressions > 0) { return round(($this->clicks / $this->impressions) * 100, 2); } return 0; }
/** * Calculate ROI if not already set */ public function calculateRoi() { if ($this->spend > 0) { return round($this->revenue / $this->spend, 2); } return 0; }
public function campaign() { return $this->belongsTo(Campaign::class); } }
|