2026-02-06 11:26:13 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Models;
|
|
|
|
|
|
|
|
|
|
// use Illuminate\Contracts\Auth\MustVerifyEmail;
|
|
|
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
2026-02-06 14:33:11 +02:00
|
|
|
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
2026-02-06 14:56:56 +02:00
|
|
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
2026-02-06 11:26:13 +02:00
|
|
|
use Illuminate\Foundation\Auth\User as Authenticatable;
|
|
|
|
|
use Illuminate\Notifications\Notifiable;
|
2026-02-14 17:06:03 +02:00
|
|
|
use Illuminate\Support\Str;
|
2026-02-06 11:26:13 +02:00
|
|
|
|
|
|
|
|
class User extends Authenticatable
|
|
|
|
|
{
|
|
|
|
|
/** @use HasFactory<\Database\Factories\UserFactory> */
|
|
|
|
|
use HasFactory, Notifiable;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* The attributes that are mass assignable.
|
|
|
|
|
*
|
|
|
|
|
* @var list<string>
|
|
|
|
|
*/
|
|
|
|
|
protected $fillable = [
|
|
|
|
|
'name',
|
|
|
|
|
'email',
|
|
|
|
|
'password',
|
2026-02-14 17:06:03 +02:00
|
|
|
'slug',
|
2026-02-06 14:33:11 +02:00
|
|
|
'avatar',
|
|
|
|
|
'bio',
|
|
|
|
|
'website',
|
|
|
|
|
'twitter',
|
|
|
|
|
'github',
|
|
|
|
|
'linkedin',
|
2026-02-06 11:26:13 +02:00
|
|
|
];
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* The attributes that should be hidden for serialization.
|
|
|
|
|
*
|
|
|
|
|
* @var list<string>
|
|
|
|
|
*/
|
|
|
|
|
protected $hidden = [
|
|
|
|
|
'password',
|
|
|
|
|
'remember_token',
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the attributes that should be cast.
|
|
|
|
|
*
|
|
|
|
|
* @return array<string, string>
|
|
|
|
|
*/
|
|
|
|
|
protected function casts(): array
|
|
|
|
|
{
|
|
|
|
|
return [
|
|
|
|
|
'email_verified_at' => 'datetime',
|
|
|
|
|
'password' => 'hashed',
|
|
|
|
|
];
|
|
|
|
|
}
|
2026-02-06 14:33:11 +02:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the roles that belong to the user.
|
|
|
|
|
*/
|
|
|
|
|
public function roles(): BelongsToMany
|
|
|
|
|
{
|
|
|
|
|
return $this->belongsToMany(Role::class);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Check if user has a specific role.
|
|
|
|
|
*/
|
|
|
|
|
public function hasRole(string $role): bool
|
|
|
|
|
{
|
|
|
|
|
return $this->roles()->where('slug', $role)->exists();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Check if user has any of the given roles.
|
|
|
|
|
*/
|
|
|
|
|
public function hasAnyRole(array $roles): bool
|
|
|
|
|
{
|
|
|
|
|
return $this->roles()->whereIn('slug', $roles)->exists();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Check if user is an admin.
|
|
|
|
|
*/
|
|
|
|
|
public function isAdmin(): bool
|
|
|
|
|
{
|
|
|
|
|
return $this->hasRole('admin');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Check if user is a moderator.
|
|
|
|
|
*/
|
|
|
|
|
public function isModerator(): bool
|
|
|
|
|
{
|
|
|
|
|
return $this->hasRole('moderator');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Check if user is a regular user.
|
|
|
|
|
*/
|
|
|
|
|
public function isUser(): bool
|
|
|
|
|
{
|
|
|
|
|
return $this->hasRole('user');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the user's avatar URL.
|
|
|
|
|
*/
|
|
|
|
|
public function getAvatarUrlAttribute(): ?string
|
|
|
|
|
{
|
|
|
|
|
if ($this->avatar) {
|
|
|
|
|
return asset('storage/avatars/' . $this->avatar);
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2026-02-06 14:56:56 +02:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the products for the user.
|
|
|
|
|
*/
|
|
|
|
|
public function products(): HasMany
|
|
|
|
|
{
|
|
|
|
|
return $this->hasMany(Product::class);
|
|
|
|
|
}
|
2026-02-06 15:13:20 +02:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the upvotes for the user.
|
|
|
|
|
*/
|
|
|
|
|
public function upvotes(): HasMany
|
|
|
|
|
{
|
|
|
|
|
return $this->hasMany(Upvote::class);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the comments for the user.
|
|
|
|
|
*/
|
|
|
|
|
public function comments(): HasMany
|
|
|
|
|
{
|
|
|
|
|
return $this->hasMany(Comment::class);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the bookmarks for the user.
|
|
|
|
|
*/
|
|
|
|
|
public function bookmarks(): HasMany
|
|
|
|
|
{
|
|
|
|
|
return $this->hasMany(Bookmark::class);
|
|
|
|
|
}
|
2026-02-14 17:06:03 +02:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Generate a unique slug for the user based on their name.
|
|
|
|
|
*/
|
|
|
|
|
public static function generateUniqueSlug(string $name): string
|
|
|
|
|
{
|
|
|
|
|
$slug = Str::slug($name);
|
|
|
|
|
$originalSlug = $slug;
|
|
|
|
|
$count = 1;
|
|
|
|
|
|
|
|
|
|
// Keep appending numbers until we find a unique slug
|
|
|
|
|
while (static::where('slug', $slug)->exists()) {
|
|
|
|
|
$slug = $originalSlug . '-' . $count;
|
|
|
|
|
$count++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $slug;
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-17 15:54:54 +03:00
|
|
|
protected static function boot()
|
|
|
|
|
{
|
|
|
|
|
parent::boot();
|
|
|
|
|
|
|
|
|
|
static::creating(function ($user) {
|
|
|
|
|
if (empty($user->slug)) {
|
|
|
|
|
$user->slug = static::generateUniqueSlug($user->name);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-14 17:06:03 +02:00
|
|
|
/**
|
|
|
|
|
* Get the route key for the model.
|
|
|
|
|
*/
|
|
|
|
|
public function getRouteKeyName(): string
|
|
|
|
|
{
|
|
|
|
|
return 'slug';
|
|
|
|
|
}
|
2026-02-06 11:26:13 +02:00
|
|
|
}
|