62 lines
1.3 KiB
PHP
62 lines
1.3 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use Illuminate\Database\Eloquent\Casts\Attribute;
|
|
use Carbon\Traits\Date;
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
|
|
|
/**
|
|
* @property int $course_id
|
|
* @property string $name
|
|
* @property string $thumbnail
|
|
* @property string $link
|
|
* @property string $status
|
|
* @property string $tracks
|
|
* @property integer $numberofchapters
|
|
* @property integer $timeswatched
|
|
* @property Date $published_at
|
|
* @property Chapter[] $chapters
|
|
*/
|
|
class Course extends Model
|
|
{
|
|
use HasFactory;
|
|
|
|
protected $fillable = [
|
|
'name',
|
|
'thumbnail',
|
|
'link',
|
|
'status',
|
|
'tracks',
|
|
'numberofchapters',
|
|
'timeswatched',
|
|
'published_at',
|
|
];
|
|
|
|
protected $appends = ['total_size', 'total_size_human'];
|
|
protected $dates = [
|
|
'published_at'
|
|
];
|
|
|
|
public function chapters(): HasMany
|
|
{
|
|
return $this->hasMany(Chapter::class);
|
|
}
|
|
|
|
public function totalSize(): Attribute
|
|
{
|
|
return Attribute::make(
|
|
get: fn() => $this->chapters->sum('video_size'),
|
|
);
|
|
}
|
|
|
|
public function totalSizeHuman(): Attribute
|
|
{
|
|
return Attribute::make(
|
|
get: fn() => formatFileSize($this->total_size),
|
|
);
|
|
}
|
|
}
|