| <?php |
|
|
| namespace App\Models; |
|
|
| use Illuminate\Database\Eloquent\Factories\HasFactory; |
| use Illuminate\Database\Eloquent\Model; |
| use Illuminate\Database\Eloquent\Relations\BelongsTo; |
|
|
| class SocialPost extends Model |
| { |
| use HasFactory; |
|
|
| protected $fillable = [ |
| 'user_id', |
| 'recurrence_campaign_id', |
| 'platform', |
| 'status', |
| 'requires_approval', |
| 'title', |
| 'caption', |
| 'hashtags', |
| 'image_url', |
| 'video_url', |
| 'scheduled_at', |
| 'recurrence_occurrence_at', |
| 'approved_at', |
| 'posted_at', |
| 'attempt_count', |
| 'next_retry_at', |
| 'external_post_id', |
| 'error_message', |
| 'meta', |
| ]; |
|
|
| protected $casts = [ |
| 'hashtags' => 'array', |
| 'meta' => 'array', |
| 'requires_approval' => 'boolean', |
| 'scheduled_at' => 'datetime', |
| 'recurrence_occurrence_at' => 'datetime', |
| 'approved_at' => 'datetime', |
| 'posted_at' => 'datetime', |
| 'next_retry_at' => 'datetime', |
| ]; |
|
|
| public function user(): BelongsTo |
| { |
| return $this->belongsTo(User::class); |
| } |
|
|
| public function recurringCampaign(): BelongsTo |
| { |
| return $this->belongsTo(SocialRecurringCampaign::class, 'recurrence_campaign_id'); |
| } |
|
|
| public function fullCaption(): string |
| { |
| $hashtags = collect($this->hashtags ?? []) |
| ->map(fn ($item) => trim((string) $item)) |
| ->filter() |
| ->map(fn ($item) => str_starts_with($item, '#') ? $item : '#'.$item) |
| ->implode(' '); |
|
|
| return trim($this->caption.($hashtags !== '' ? "\n\n{$hashtags}" : '')); |
| } |
| } |
|
|