Files
dredgegram/app/Models/Post.php
T
dredgy b2f2d8b104
linter / quality (push) Canceled after 0s
tests / ci (8.3) (push) Canceled after 0s
tests / ci (8.4) (push) Canceled after 0s
tests / ci (8.5) (push) Canceled after 0s
Initial Commit
2026-08-29 00:17:41 +10:00

81 lines
2.0 KiB
PHP

<?php
namespace App\Models;
use App\Observers\PostObserver;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Attributes\ObservedBy;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Model;
#[ObservedBy(PostObserver::class)]
class Post extends Model
{
protected $table = 'posts';
// Instagram's own post pk is the primary key, not auto-increment
protected $keyType = 'string';
public $incrementing = false;
// scraped_at is set by the DB default; created_at/updated_at don't apply
public $timestamps = false;
protected $fillable = [
'id',
'code',
'username',
'full_name',
'caption',
'accessibility_caption',
'is_video',
'is_carousel',
'like_count',
'comment_count',
'taken_at',
'image_url',
'video_url',
'carousel_images',
'scraped_at',
'profile_pic_url',
];
protected $casts = [
'is_video' => 'boolean',
'is_carousel' => 'boolean',
'like_count' => 'integer',
'comment_count' => 'integer',
'taken_at' => 'integer',
'carousel_images' => 'array',
'scraped_at' => 'datetime',
'taken_at_date' => 'datetime',
'permalink' => 'string',
];
protected $appends = [
'permalink',
'taken_at_date',
];
/**
* Instagram's taken_at is a unix timestamp - convenience accessor
* for an actual Carbon instance.
*/
protected function takenAtDate(): Attribute
{
return Attribute::make(
get: fn () => $this->taken_at ? Carbon::createFromTimestamp($this->taken_at) : null,
);
}
/**
* Convenience: the actual instagram.com URL for this post.
*/
protected function permalink(): Attribute
{
return Attribute::make(
get: fn () => $this->code ? "https://www.instagram.com/p/{$this->code}/" : null,
);
}
}