refactor: susun semula struktur folder — Laravel source ke src/
This commit is contained in:
67
src/app/Models/Certificate.php
Normal file
67
src/app/Models/Certificate.php
Normal file
@@ -0,0 +1,67 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
class Certificate extends Model
|
||||
{
|
||||
protected $fillable = [
|
||||
'uuid', 'program_id', 'participant_id', 'certificate_template_id',
|
||||
'certificate_no', 'file_path', 'token', 'status', 'error_message',
|
||||
'generated_at', 'emailed_at', 'downloaded_at', 'download_count',
|
||||
];
|
||||
|
||||
protected function casts(): array
|
||||
{
|
||||
return [
|
||||
'generated_at' => 'datetime',
|
||||
'emailed_at' => 'datetime',
|
||||
'downloaded_at' => 'datetime',
|
||||
'download_count' => 'integer',
|
||||
];
|
||||
}
|
||||
|
||||
protected static function boot(): void
|
||||
{
|
||||
parent::boot();
|
||||
static::creating(function ($model) {
|
||||
$model->uuid ??= (string) Str::uuid();
|
||||
$model->token ??= Str::random(48);
|
||||
});
|
||||
}
|
||||
|
||||
public function program()
|
||||
{
|
||||
return $this->belongsTo(Program::class);
|
||||
}
|
||||
|
||||
public function participant()
|
||||
{
|
||||
return $this->belongsTo(Participant::class);
|
||||
}
|
||||
|
||||
public function template()
|
||||
{
|
||||
return $this->belongsTo(CertificateTemplate::class, 'certificate_template_id');
|
||||
}
|
||||
|
||||
public function emailLogs()
|
||||
{
|
||||
return $this->hasMany(EmailLog::class);
|
||||
}
|
||||
|
||||
public function isGenerated(): bool
|
||||
{
|
||||
return $this->status === 'generated' || $this->status === 'emailed' || $this->status === 'downloaded';
|
||||
}
|
||||
|
||||
public function recordDownload(): void
|
||||
{
|
||||
$this->increment('download_count');
|
||||
if (! $this->downloaded_at) {
|
||||
$this->update(['downloaded_at' => now(), 'status' => 'downloaded']);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user