43 lines
859 B
PHP
43 lines
859 B
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
|
|
|
class Task extends Model
|
|
{
|
|
protected $guarded = ['id'];
|
|
protected $casts = [
|
|
'created_at' => 'datetime',
|
|
'completed_at' => 'datetime'
|
|
];
|
|
|
|
public function list(): BelongsTo
|
|
{
|
|
return $this->belongsTo(TaskList::class);
|
|
}
|
|
|
|
public function complete(): void
|
|
{
|
|
$this->update([
|
|
'completed' => true,
|
|
'completed_at' => now()->toDateTimeString()
|
|
]);
|
|
}
|
|
|
|
public function toggleComplete(): self
|
|
{
|
|
if (!$this->completed) {
|
|
$this->complete();
|
|
} else {
|
|
$this->update([
|
|
'completed' => false,
|
|
'completed_at' => null
|
|
]);
|
|
}
|
|
|
|
return $this;
|
|
}
|
|
}
|