flowtodo/app/Models/TaskList.php
Javier Feliz 0ddd325281
Some checks are pending
linter / quality (push) Waiting to run
tests / ci (push) Waiting to run
Initial commit
2025-07-15 18:28:47 -04:00

44 lines
986 B
PHP

<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Support\Facades\Auth;
class TaskList extends Model
{
protected $guarded = ['id'];
protected static function boot(): void
{
parent::boot();
static::saving(function ($model) {
$slug = str($model->name)->slug()->toString();
$exists = Auth::user()
->taskLists()
->where('slug', $slug)
->where('id', '!=', $model->id)
->exists();
if ($exists) {
$slug = str($slug)->append('-' . uniqid());
}
$model->slug = $slug;
});
}
public function setAsHome()
{
Auth::user()->taskLists()->update(['is_home' => false]);
$this->update(['is_home' => true]);
}
public function tasks(): HasMany
{
return $this->hasMany(Task::class);
}
}