Files
trackbugs/app/Models/User.php
T
2026-03-21 15:25:05 +02:00

75 lines
2.2 KiB
PHP

<?php
namespace App\Models;
// use Illuminate\Contracts\Auth\MustVerifyEmail;
use Database\Factories\UserFactory;
use Illuminate\Database\Eloquent\Attributes\Fillable;
use Illuminate\Database\Eloquent\Attributes\Hidden;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\MorphMany;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Illuminate\Support\Str;
#[Fillable(['name', 'email', 'password'])]
#[Hidden(['password', 'remember_token'])]
class User extends Authenticatable
{
/** @use HasFactory<UserFactory> */
use HasFactory, Notifiable;
protected function casts(): array
{
return [
'email_verified_at' => 'datetime',
'password' => 'hashed',
];
}
public function projects(): BelongsToMany
{
return $this->belongsToMany(Project::class)
->withPivot('role')
->withTimestamps();
}
public function ownedProjects(): BelongsToMany
{
return $this->projects()->wherePivot('role', 'owner');
}
public function notificationPreferences(): HasMany
{
return $this->hasMany(NotificationPreference::class);
}
public function notificationPreferenceFor(Project $project): NotificationPreference
{
return $this->notificationPreferences()
->firstOrCreate(
['project_id' => $project->id],
['email_enabled' => true, 'database_enabled' => true, 'muted' => false],
);
}
public function tokens(): MorphMany
{
return $this->morphMany(PersonalAccessToken::class, 'tokenable');
}
public function createToken(string $name, array $abilities = ['*'], ?\DateTimeInterface $expiresAt = null): PersonalAccessToken
{
$token = Str::random(64);
return $this->tokens()->create([
'name' => $name,
'token' => hash('sha256', $token),
'abilities' => $abilities,
'expires_at' => $expiresAt,
]);
}
}