user profiles

This commit is contained in:
2026-02-06 14:33:11 +02:00
parent d458e36f4f
commit b2fd2a5eac
20 changed files with 3936 additions and 9 deletions
+28
View File
@@ -0,0 +1,28 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
class Role extends Model
{
/**
* The attributes that are mass assignable.
*
* @var array<int, string>
*/
protected $fillable = [
'name',
'slug',
'description',
];
/**
* Get the users that belong to the role.
*/
public function users(): BelongsToMany
{
return $this->belongsToMany(User::class);
}
}
+66
View File
@@ -4,6 +4,7 @@ namespace App\Models;
// use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
@@ -21,6 +22,12 @@ class User extends Authenticatable
'name',
'email',
'password',
'avatar',
'bio',
'website',
'twitter',
'github',
'linkedin',
];
/**
@@ -45,4 +52,63 @@ class User extends Authenticatable
'password' => 'hashed',
];
}
/**
* 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;
}
}