You've already forked Atomcms-edit
64 lines
1.4 KiB
PHP
Executable File
64 lines
1.4 KiB
PHP
Executable File
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
|
|
|
/**
|
|
* @method static \Illuminate\Database\Eloquent\Builder<static>|RadioHistory where($column, $operator = null, $value = null)
|
|
* @method static \Illuminate\Database\Eloquent\Builder<static>|RadioHistory create($attributes = [])
|
|
*/
|
|
class RadioHistory extends Model
|
|
{
|
|
#[\Override]
|
|
protected $table = 'radio_history';
|
|
|
|
#[\Override]
|
|
protected $guarded = ['id', 'created_at', 'updated_at', 'dj_id', 'song'];
|
|
|
|
/** @var array<string, string> */
|
|
#[\Override]
|
|
protected $casts = [
|
|
'started_at' => 'datetime',
|
|
'ended_at' => 'datetime',
|
|
];
|
|
|
|
public function user(): BelongsTo
|
|
{
|
|
return $this->belongsTo(User::class, 'user_id');
|
|
}
|
|
|
|
public function scopeRecent($query)
|
|
{
|
|
return $query->orderBy('started_at', 'desc');
|
|
}
|
|
|
|
public function scopeActive($query)
|
|
{
|
|
return $query->whereNull('ended_at');
|
|
}
|
|
|
|
public function getDurationAttribute(): ?string
|
|
{
|
|
if (! $this->ended_at) {
|
|
return null;
|
|
}
|
|
|
|
$diff = $this->started_at->diff($this->ended_at);
|
|
|
|
if ($diff->h > 0) {
|
|
return $diff->h . ' uur ' . $diff->i . ' min';
|
|
}
|
|
|
|
return $diff->i . ' minuten';
|
|
}
|
|
|
|
public function endSession(): void
|
|
{
|
|
$this->update([
|
|
'ended_at' => now(),
|
|
]);
|
|
}
|
|
}
|