aiblog/app/Models/User.php
2024-11-17 11:30:01 +08:00

54 lines
1.1 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Laravel\Sanctum\HasApiTokens;
use Illuminate\Database\Eloquent\Relations\HasMany;
class User extends Authenticatable
{
use HasApiTokens, HasFactory, Notifiable;
protected $fillable = [
'account',
'name',
'email',
'password',
'role',
];
protected $hidden = [
'password',
'remember_token',
];
protected $casts = [
'email_verified_at' => 'datetime',
'password' => 'hashed',
];
public function posts(): HasMany
{
return $this->hasMany(Post::class, 'author_id');
}
public function comments(): HasMany
{
return $this->hasMany(Comment::class, 'author_id');
}
public function isAdmin(): bool
{
return $this->role === 'admin';
}
public function isAuthor(): bool
{
return $this->role === 'author';
}
}