44 lines
986 B
PHP
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);
|
|
}
|
|
}
|