63 lines
1.4 KiB
PHP
63 lines
1.4 KiB
PHP
<?php
|
|
|
|
namespace App\Domains\Link\Models;
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
|
|
|
class Link extends Model
|
|
{
|
|
use SoftDeletes;
|
|
|
|
protected $guarded = ['id'];
|
|
|
|
protected static function booted(): void
|
|
{
|
|
static::creating(function (self $model) {
|
|
if (empty($model->ulid)) {
|
|
$model->ulid = \Illuminate\Support\Str::ulid();
|
|
}
|
|
});
|
|
}
|
|
|
|
protected $casts = [
|
|
'rules' => 'array',
|
|
'pixel_config' => 'array',
|
|
'tags' => 'array',
|
|
'expires_at' => 'datetime',
|
|
];
|
|
|
|
public function workspace(): BelongsTo
|
|
{
|
|
return $this->belongsTo(\App\Domains\Workspace\Models\Workspace::class);
|
|
}
|
|
|
|
public function domain(): BelongsTo
|
|
{
|
|
return $this->belongsTo(\App\Domains\Domain\Models\Domain::class);
|
|
}
|
|
|
|
public function variants(): HasMany
|
|
{
|
|
return $this->hasMany(LinkVariant::class);
|
|
}
|
|
|
|
public function clicks(): HasMany
|
|
{
|
|
return $this->hasMany(\App\Domains\Analytics\Models\Click::class);
|
|
}
|
|
|
|
public function isActive(): bool
|
|
{
|
|
if ($this->status !== 'active') {
|
|
return false;
|
|
}
|
|
if ($this->expires_at && $this->expires_at->isPast()) {
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
}
|